# Unity Topdown movement faster when moving diagonally

Hi! Im using this code to move my character in my topdown 2D game. But when i move diagonally it moves way faster then when i move forward and on the sides.

``````        //Movement of player
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime * moveSpeed);
``````

Anyone know how to fix this?

The reason for this is that you are getting two inputs at once, and so your speed doubles on a keyboard, and goes up incrementally on a joystick. To fix this the best way would be to divide the final vector by two if both are greater than or equal to 1. Like this:

` if(Input.GetAxisRaw("Horizontal") >= 1 && Input.GetAxisRaw("Vertical") >= 1) { Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal") / 2, Input.GetAxisRaw("Vertical") / 2); }`

The answer, as others have mentioned is that when you move 1 unit horizontally and 1 vertically, you move too far diagonally. As @toddisarockstar’s mentioned .7 on the X plus .7 on the Y gives a diagonal of approximately 1 unit, which is what you need to aim for.

The way I do it though is that if the magnitude of your movement vector is greater than 1, then divide your movement vector by the magnitude. This normalizes it to a magnitude of 1, but keeps your vector going in the proper direction it should.

Now that method is good for Joysticks, and will also work for keyboard as well, but if you only have keyboard input, you can just simply use Normalize() on your movement vector. This doesn’t work for joystick input though, if you want to be able to move the joystick slightly to move small amounts, thus why I explained the magnitude way.

Normalize() is a method of a Vector 2/3, and magnitude is a property of a Vector 2/3.

You can also clamp the movement_vector to make it not go over 1. Joystick-friendly as well.

Here’s my code:

``````void Update()
{
if (moving == true)
{
movementSpeed = baseSpeed * boostSpeed; //Player Movement Speed
}
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
}
void FixedUpdate()
{
movementVector = Vector2.ClampMagnitude(movement, 1); //Set the maximum value to 1 so it never goes over. This essentially sets the vector value to (±0.7,±0.7) when moving diagonally.

newPosition = playerRigidbody.position + movementVector * movementSpeed * Time.fixedDeltaTime;

playerRigidbody.MovePosition(newPosition); //Move playerRigidBody towards the new position

playerVelocity = (newPosition - playerRigidbody.position) * Time.fixedDeltaTime; //Get velociity by subtracting the current position from the future position

if (playerVelocity.magnitude > 0) //If the lenght of the velocity vector is greater than 0, it means it is moving.
{
moving = true;
}
else
{
moving = false;
}

}
``````

I wanted to get the velocity to check if the player was moving or not, and also to get the value itself for later uses in the game. If you don’t need to get the velocity, you can simplify the code.