应在 Start、Update 或 FixedUpdate 函数中对 Rigidbody 施加力

Should force be added force to Rigidbody in the Start, Update or FixedUpate function

我是 Unity 新手。

我有 Rigidbody2D,我想向我的播放器添加常量 velocity。 我想知道在 StartUpdate(或 FixedUpate

中将速度应用于 Rigidbody2D 有什么区别或 advantages/disadvantages 或 bast 练习

当我在开始时应用速度时一切正常,那么为什么我们每帧都添加速度(Update 方法)?

I want to add constant velocity to my player.

Start function is eliminated in this case since it's called once only when the script is GameObject and script enabled. If you need to apply force over and over again then the Start 函数不是您应该使用的东西。

Update函数用于每一帧做任何事情。例如,每帧手动移动对象。

FixedUpdate 函数用于对 Rigidbody 做一些事情,这包括在每个固定帧为其添加力。这是您需要使用的,因为您正在对 Rigidbody 对象执行某些操作。

您没有提到的另一个是 LateUpdate 函数。当您想让 GameObject 跟随相机时使用它,因为它是在调用所有 Update 函数之后调用的。


Unity 也有一个 ConstantForce 实用程序来简化向 Rigidbody 添加恒力的过程。有了它,您可以在 Start 函数中向 ConstantForce 添加一次力,它会处理其余的直到您更改力。

ConstantForce2D cForce = targetObj.GetComponent<ConstantForce2D>();
cForce.force = new Vector2(0, 100);