Unity对象不旋转

时间:2019-05-25 15:44:19

标签: c# unity3d

我正在尝试在特定边界内旋转对象,但是该对象不会旋转。

public class shincon : MonoBehaviour
{
    Rigidbody rb2;
    float shinspeed = 10;

    // Use this for initialization
    void Start()
    {
        rb2 = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float Posax = Input.GetAxis("leftshin");
        if (Posax != 0)
        {
            Vector3 move = new Vector3(shinspeed * Posax * Time.deltaTime, 0, 0);
            transform.rotation = Quaternion.identity;
            Vector3 euler = transform.rotation.eulerAngles;
            float clampx = Mathf.Clamp(move.x + euler.x, 0, 160);
            Vector3 ready = new Vector3(clampx - euler.x, 0, 0);
            Quaternion rmove = Quaternion.Euler(ready);

            rb2.MoveRotation(rb2.rotation * rmove);
        }
    }
}

没有语法错误,但不会旋转。

1 个答案:

答案 0 :(得分:1)

行后

transform.rotation = Quaternion.identity;

下一个

Vector3 euler = transform.rotation.eulerAngles;

将始终返回0,0,0

进一步

Quaternion rmove = Quaternion.Euler(ready);

最后得到Quaternion.Identity的结果

rb2.MoveRotation(rb2.rotation * rmove);

总是也会产生Quaternion.Identity,因此该对象将永远不会旋转。


删除行

transform.rotation = Quaternion.identity;