I'm making a topdown 2D RPG in Unity. All moveable objects have Rigidbody2D-components with gravity set to 0, linear drag set to 5 and different masses. Walls are stationary boxes with BoxCollider2Ds.

Here's my problem: http://a.pomf.se/nifmcn.webm The rock is not particularly easy to see, but if you watch when I'm trying to walk into a wall, the player kind of jags in and out of the wall.

Of course, this is because I'm moving the player by moving it's Rigidbody2D, for example:

rigidbody2D.transform.position += Vector3.up*Time.deltaTime*speed;

The box then throws the player in the opposite direction of what he was going.

What's a good way of stopping a player from moving in the direction he's trying to go? More specifically, how do you detect which way he's going? Do you find the angle between the player and the object/wall?

This is kind of an open question, but I think it's extremely important.

  • \$\begingroup\$ rigidbody2D.tranform.position isn't using the Rigidbody... It is equivalent to saying transform.position etc... You need to use rigidbody.velocity instead, that is if you were wanting to alter its rigidbody... \$\endgroup\$
    – Savlon
    Feb 9, 2015 at 5:11

1 Answer 1


Don't set the position explicitly.

When you set properties like position and even velocity, you do so outside the physics loop, so it has no chance to correct itself within the current frame, so you see jittering behaviour. In other words, in the current frame you'll reposition the body, possibly colliding into another body, and the physics system must correct this in the next frame. Consider this passage from the Rigidbody.velocity manual page:

In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation.

If possible, always try to do it the recommended way - by adding forces by using functions like AddForce. This lets the physics system apply the right transformations, without temporary collisions.

This answer also contains similar advice.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .