Unity 3D中Rigidbody.AddForce无法移动对象问题求助
问题:玩家控制器对象无法移动
我编写了以下玩家控制器代码,同时附上对象属性截图,但尝试移动对象时没有任何反应,求解决方法。
控制器代码
public float speed = 50.0f; private float turnSpeed = 45.0f; private float horizontalInput; private float forwardInput; public bool hasDelivery = false; public bool hasPowerUp = false; public GameManager1 gameManager; private Rigidbody rb; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); gameManager = GameObject.Find("GameManager").GetComponent<GameManager1>(); } void FixedUpdate() { forwardInput = Input.GetAxis("Vertical"); horizontalInput = Input.GetAxis("Horizontal"); Vector3 movement = transform.forward * forwardInput * speed; rb.AddForce(movement * speed); float turn = horizontalInput * turnSpeed * Time.fixedDeltaTime; Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f); //rb.AddTorque(transform.up * turn); rb.MoveRotation(rb.rotation * turnRotation); }
对象属性截图

排查与解决建议
- 检查Rigidbody组件:确保对象上挂载了Rigidbody,且
Is Kinematic未勾选。若组件不存在,rb会为空,导致运动逻辑失效。 - 修正移动力计算:代码中
rb.AddForce(movement * speed)重复乘以了speed,应改为rb.AddForce(movement * Time.fixedDeltaTime),保证运动帧率无关。 - 验证输入轴设置:在Unity的
Edit > Project Settings > Input Manager中,确认Vertical、Horizontal轴的按键绑定正确(对应WASD或方向键)。 - 调整物理参数:若Rigidbody的Mass值过大,当前
speed可能不足以推动对象,可尝试调大speed或减小Mass。 - 确认脚本挂载:确保脚本已挂载在目标对象上,且对象处于激活状态(Active勾选)。
- 检查场景碰撞:若对象被其他碰撞体卡住,暂时移开场景中的其他物体测试是否能正常移动。
内容的提问来源于stack exchange,提问作者Will Schultz
相关产品推荐
相关产品推荐

