Question Best convention for player movement.

Discussion in 'Physics' started by Renatusdev, Nov 17, 2020.

1. Renatusdev

Joined:
Dec 9, 2018
Posts:
35
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.

Last edited: Nov 17, 2020
2. rubcc95

Joined:
Dec 27, 2019
Posts:
222
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.

3. rubcc95

Joined:
Dec 27, 2019
Posts:
222
Ups, the scene, forgot it. Here it is.

File size:
959 bytes
Views:
289