【问题标题】:Floating Problem With Rigidbody.Velocity in UnityUnity 中刚体.速度的浮动问题
【发布时间】:2019-11-03 17:35:40
【问题描述】:

下面是我用来在 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);
    }

}

【问题讨论】:

    标签: c# unity3d rigid-bodies


    【解决方案1】:

    transform.forwardVector3(0,0,1),这就是问题所在。

    您将 y 速度设置为 0。

    【讨论】:

    • 没有解决问题,我的对象仍然需要很长时间才能到达地面。感觉像是在漂浮,但实际上它正在非常缓慢地移动到水面。
    • 好的,这适用于下降部分,但现在它锁定了它只移动到 x 轴。
    • 不,伙计,仍然只锁定一个方向。
    • 现在这是另一个问题,问题只是 Y,其他的东西你需要自己弄清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多