Unity 中 Rigidbody.Velocity 的浮动问题
Floating Problem With Rigidbody.Velocity in Unity
下面是我用来在 Unity 中移动对象的代码。 rb.Velocity
行使我的对象在游戏模式中浮动。如果我注释掉这条线,那么物体就会掉下来。
有人可以解释一下这里发生了什么吗?
public class PlayerController : MonoBehaviour
{
public float forwardVelocity = 0F;
public float maxSpeed = 180;
public float acceleratePerSecond = 8.0F;
public float rotateSpeed = 3.0F;
private float yaw = 0.0f;
private float pitch = 0.0f;
protected Rigidbody rb;
float timeZeroToMax = 2.5F;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
acceleratePerSecond = maxSpeed / timeZeroToMax;
forwardVelocity = 0F;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
{
if (forwardVelocity< maxSpeed)
{
forwardVelocity += acceleratePerSecond * Time.deltaTime;
}
}
forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);
rb.velocity = transform.forward * forwardVelocity;
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
yaw += rotateSpeed * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
transform.forward
是一个Vector3(0,0,1)
,就是这个问题。
您正在将 y 速度设置为 0。
下面是我用来在 Unity 中移动对象的代码。 rb.Velocity
行使我的对象在游戏模式中浮动。如果我注释掉这条线,那么物体就会掉下来。
有人可以解释一下这里发生了什么吗?
public class PlayerController : MonoBehaviour
{
public float forwardVelocity = 0F;
public float maxSpeed = 180;
public float acceleratePerSecond = 8.0F;
public float rotateSpeed = 3.0F;
private float yaw = 0.0f;
private float pitch = 0.0f;
protected Rigidbody rb;
float timeZeroToMax = 2.5F;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
acceleratePerSecond = maxSpeed / timeZeroToMax;
forwardVelocity = 0F;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
{
if (forwardVelocity< maxSpeed)
{
forwardVelocity += acceleratePerSecond * Time.deltaTime;
}
}
forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);
rb.velocity = transform.forward * forwardVelocity;
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
yaw += rotateSpeed * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
transform.forward
是一个Vector3(0,0,1)
,就是这个问题。
您正在将 y 速度设置为 0。