首页 > 解决方案 > 我的 y 旋转一直在变化,即使我没有改变它

问题描述

我正在尝试将其 x 和 z 轴旋转到鼠标位置,同时不更改 y。由于某种原因,y一直在变化!!!我当前的代码看起来像这样

    public float moveSpeed = 5;
    public float maxMoveSpeed = 15;
    CharacterController body;
    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
             Vector3 point = new Vector3(hit.point.x, 0, hit.point.z);
            print(point);
            transform.LookAt(point);

        }
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");


        Vector3 moveDis = transform.forward * v * moveSpeed;
        moveDis += transform.right * h * moveSpeed;
        body.SimpleMove(moveDis);
    }

出于某种原因,如果我删除播放器控制器它工作正常!

标签: c#unity3drotation

解决方案


我正在尝试旋转它的 x 和 z 轴

不,你不是^^

你正在做的是

Vector3 point = new Vector3(hit.point.x, 0, hit.point.z);

transform.LookAt(point);

在哪里LookAt

旋转变换,使前向向量指向 /target/ 的当前位置。

在大多数情况下,它将围绕XY轴旋转您的对象!


不过,目前尚不清楚您的实际目标是什么,以便为此提供可能的解决方案。


推荐阅读