LookAtMousePosition会干扰基本的(垂直,水平)Vector3运动

时间:2018-09-21 23:37:36

标签: c# unity3d

不想花很多时间,所以我会尽量简短。

我有2个空白)(功能),这些功能使游戏具有运动机制。 第一个是LookAtMousePosition旋转机械手,第二个是基本的Vector3获得“水平”并获得垂直运动。 没有LookAtMousePosition函数,我可以完美移动。但是,当我添加LookAtMousePosition时,我只能跳跃并且几乎不能移动。

这是我的运动脚本的完整代码:

[RequireComponent(typeof(PlayerMotor))]
public class janitor : MonoBehaviour {
private Camera mainCamera;

[SerializeField]
private float speed = 5f;

private PlayerMotor motor;
// Use this for initialization
void Start () 
{
    motor = GetComponent<PlayerMotor>();
    mainCamera = FindObjectOfType<Camera>();
}

void LookAtPositionOfMouse()
{
    Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
    Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    float raylength;

    if (groundPlane.Raycast(cameraRay, out raylength))
    {
        Vector3 pointToLook = cameraRay.GetPoint(raylength);
        Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);

        transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
    }

}
// Update is called once per frame
void Update () 
{
    float xMov = Input.GetAxisRaw("Horizontal");
    float zMov = Input.GetAxisRaw("Vertical");

    Vector3 moveHorizontal = transform.right * xMov; //basic vector3 controls //(1,0,0)
    Vector3 moveVertical = transform.forward * zMov;// (0,0,1)

    //final movement vector. Normalised.
    Vector3 velocity = (moveHorizontal + moveVertical).normalized * speed; //normalises the length and speed of both vector3 variables above

    //Apply Movement
    motor.Move(velocity);

}

void FixedUpdate()
{
    //LookAtPositionOfMouse();
}
void LateUpdate()
{

}
}

这是在另一个脚本中启动运动(而不是旋转)的代码:

 private Vector3 _velocity = Vector3.zero;
 public void Move(Vector3 velocity)
{
    //gets movement vector
    _velocity = velocity;
}
 void PerformMovement()
{
    if (_velocity != Vector3.zero)
    {
        rb.MovePosition(rb.position + _velocity * Time.deltaTime);
    }
}

我对如何解决这个问题迷失了。我该怎么办? )

0 个答案:

没有答案