尽管冻结,Unity 对象仍会旋转

问题描述

我正在 Unity 中制作游戏,但是对象旋转出现问题:我的坐标被冻结,但移动对象后它仍然旋转:

https://youtu.be/tVwGnWVi_Eo

在视频中,我移动了对象,当我释放所有键时,尽管有冻结选项,它仍会旋转。

这是我的运动脚本:

public class Movement : MonoBehavIoUr
{
     public int speed;
     public int turningForce;
     private int jumpForce;
     public int jumpForceMax;

     public Rigidbody rb;

     bool doJump;

     void Start()
     {

     }

     private void Update()
     {
         if (Input.GetKeyUp("space"))
         {
             doJump = true;
         }
     }

     void FixedUpdate()
     {
         if (Input.GetKey("space") && IsOnGround)
         {
             if (jumpForce < jumpForceMax)
             {
                 jumpForce += 50;
             }
         }

         if (Input.GetKey("w") && IsOnGround)
         {
             rb.AddRelativeForce(speed * Time.deltaTime,0);
         }

         if (Input.GetKey("a"))
         {
             rb.AddTorque(new Vector3(0,-speed * Time.deltaTime,0));
         }

         if (Input.GetKey("d"))
         {
             rb.AddTorque(new Vector3(0,speed * Time.deltaTime,0));
         }

         if (doJump)
         {
             rb.AddForce(0,jumpForce,0);
             doJump = false;
             jumpForce = 0;
         }
     }

     private bool m_IsOnGround;

     public bool IsOnGround
     {
         get
         {
             if (m_IsOnGround)
             {
                 m_IsOnGround = false;
                 return true;
             }
             else
             {
                 return false;
             }
         }
     }

     void OnCollisionStay()
     {
         m_IsOnGround = true;
     }
 }

如您所见,没有变换位置更改,并且启用了冻结 Y 选项。

希望有人能帮助我了解正在发生的事情。

解决方法

我不确定你是在哪里冻结了 Y 旋转但是

rb.AddTorque(new Vector3(0,-speed * Time.deltaTime,0));

是一个使用力旋转刚体的函数 如果你冻结了旋转,这有什么意义?

enter image description here

如果你像这样冻结了组件的旋转,函数 AddTorque() 不起作用。