首页 > 解决方案 > 玩家在 Unity 中不动

问题描述

这是我的代码:

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 4000f;
    public float sidewaysForce = 100f;

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.   
    void FixedUpdate()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime); 

        if( Input.GetKey("d") )
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if( Input.GetKey("a") )
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

Unity - 玩家不动

请帮忙。

标签: c#visual-studiounity3d

解决方案


让我们尝试一些事情。首先,尝试在 FixedUpdate 中取出 Time.deltaTime。在 FixedUpdate 中添加力时,通常不需要使用Time.deltaTime。

其次,尝试创建一个零摩擦的物理材质并将其附加到玩家对象的盒子碰撞器上。


推荐阅读