Manually Changing Transform or Setting Rigidbody Velocity to move an NPC?

I am making a Top-Down game that consists of a player and an enemy (NPC). I am trying to program the NPC to move and behave in certain ways. I know there is a couple ways to move a Game Object: via Transform or Rigidbody. Here comes the question, which method is the better practice to move an NPC?

Make the NPC a rigidbody and move it with AddForce or MovePosition. Moving objects by changing their transform’s position isn’t something you should do because it overrides the physics engine’s attempts at preventing your objects from passing through walls etc.

What about changing the Rigidbody Velocity directly? Does it differ from AddForce or MovePosition?

Setting the velocity is okay but be aware that if you set it every frame then your character won’t have the ability to be knocked around by other physics items or be carried along by platforms. But then this also applies to MovePosition.

It seems lots of developers don’t like AddForce because it gives their character a weighty feel from the inertia and momentum and they probably associate that feeling with lag. You can sometimes get around this by reducing the mass of the character’s rigidbody to make it feel much lighter.

2 Likes

When making a top-down game, make sure you use the 2D physics when possible. That tends to end up with better performance, and fewer glitches. Might be easier to configure all the joints too.

In case you need 3D, it’s also worth mentioning that whenever you change transform, it’s an instant teleportation as seen by the physics engine. Teleportations are best avoided because they break joints, violate constraints and introduce instabilities.

Speaking of controlling force vs velocity, that tends to be a personal preference since in practice you won’t change velocity directly (as mentioned above, you’d normally like to support platforms and reaction to the surrounding objects), instead you’d add impulse. Impulse and force are related.

Anthony.

1 Like