Best convention for player movement.

I’m lost in trying to choose the best way to move a 2D character using Rigidbody2D.

First I read the MovePosition() method in Rigidbody should only be called once per frame.

  • Before I had Movement using MovePosition() + also Manual Gravity using MovePosition()
  • Now I’m setting everything up to use one vector containing the full movement that frame and putting it in MovePosition()

Now I’m reading that the velocity property of Rigidbody can be used. Or the AddForce().

I’m confused. How do I move my player the way it should be using Rigidbody2D.

MovePosition() for horizontal movement ? then adding AddForce() for jump and gravity ? Or stacking vectors and putting them in velocity?

I just want to know the best approach.

EDIT: I just learned AddForce() overrides MovePosition. I’m like, confused x2.

AddForce is used for apply a force follow the physics rules.
MovePosition is used in order to move the rigidbody without follow the physics rules, but still interacting properly with other colliders. Basically Rigidbody.MovePosition(a) its the same than transform.position = a, but keeping the physics interactions.

For an horizontal move if you Add a force in right direction, you will add a velocity to your rigidbody and when you stop, it still will be moving unitl it speed drops (if it has some drag). With MovePosition it will be moved to the position u asked for ignoring mass, current velocity and any physics (and without modify them).

For jump you would like to use AddForce() 99% of times, since you want a realistic physics jumps…

I won’t explain more. Just test the attached scene to this post. Gameobject A (red )uses MovePosition(), GameObject B (green) uses AddForce(). Move them with your arrow and you will realize about what I mean.

B may seems pretty uncontrollable, but setting it speed and rigidbody drag to different values you can get other results. Try it.

Ups, the scene, forgot it. Here it is.

6531551–737795–rbExample.unitypackage (959 Bytes)