# 2d topdown physics

What is the best practice for moving player in a 2d topdown game?

This is my movement script attached to player;

``````public class MovementScript : MonoBehaviour {

public Vector2 speed = new Vector2(25, 25);

private Vector2 movement;

void Update()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");

movement = new Vector2(speed.x * inputX, speed.y * inputY);

if (inputX != 0 && inputY != 0) {
movement *= 0.707106781f;
}
}

void FixedUpdate()
{
rigidbody2D.velocity = movement;
//which one is preferrable?
}
}
``````

Also, there are two players in the scene. Both are cylinders with Circle collider and rigidbody 2d. When a player hits the other one, there is a strange behaviour that it feels like two players become together.

For example, i am going right and when i hit the other player on the left and continue to go right, we both go right. Thats okay, however while going right, if i press the up button, the other player goes up too. It feels like two players collide and stick together. This is not normal in 2d. What can i do to solve this?

If you need anything, please tell me and i will post here (settings, etc.)

Thanks for answers. I have investigated this issue and here are my findings in case anyone goes through same problems:

• The code is okay.

In FixedUpdate:

`````` rigidbody2D.velocity = movement;
``````

is preferable if you want to change the direction of character as soon as the user presses a direction key. However if you want to achieve real physics (eg. momentum) you should use;

``````rigidbody2D.AddForce (movement);
``````

Using AddForce applies the force to the object. The object already has a direction and speed, and by using AddForce you add another force to the objectsâ€™s current momentum. So you can achieve more realistic physics this way. However the object will not immediately respond to the direction buttons pressed, because it has a momentum.

• Two objects that are dragging each other in an undesirable way is the result of friction. Add a 2D Physics Material to Rigidbody component of that object and set Friction to 0 for the physics material. You will see that objects will no longer act like they are sticking to each other when they hit each other.

You could freeze the position for one of the axis under the inspector for all of your objects. That should limit them to moving in 2D.