【发布时间】:2021-02-04 00:47:59
【问题描述】:
我在 Unity 中制作游戏,但是对象旋转出现问题:我的坐标冻结了,但我移动对象后它仍然旋转:
在视频中,我移动了对象,当我释放所有键时,尽管有“冻结”选项,它仍会旋转。
这是我的运动脚本:
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, 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;
}
}
如您所见,没有 transform.position 更改,并且启用了 Freeze Y 选项。
希望有人可以帮助我了解发生了什么。
【问题讨论】:
标签: c# unity3d rotation game-physics