2D rigidbody top down movement problem

Hello, I am making a 2d game and i had a problem with controls. I am trying to make it so player can only move horizontally and vertically, and if i press 2 control buttons at the same time, for example w and d, make player move in direction which second button u pressed coresponds, it works fine with Vertical, but if I press for example d while moving up it just keeps moving upwards, while it has to move right. Here is the code:

public class Controls : MonoBehaviour
 {
  public Vector2 movement;
  void Update()
  {
    movement.x = Input.GetAxisRaw("Horizontal");
    movement.y = Input.GetAxisRaw("Vertical");
    if (movement.y != 0)
    {
        movement.x = 0;
    }
    if (movement.x != 0)
    {
        movement.y = 0;
  }
 }

Hi @Keyakit , This is very Easy you should use Horizontal and vertical Axis which is very common in between game developers:
I encourage you to watch this video because he explains 2d movement for beginners very well:
if you had any other problems feel free to ask :slight_smile:

It is working:

    public Vector2 movement;
    bool isVerticalMovement, isVerticalDown, isHorizontalDown;
    void Update()
    {
        if (Input.GetButtonDown("Vertical"))
        {
            isVerticalDown = true;
            isVerticalMovement = true;
        }
        if (Input.GetButtonDown("Horizontal"))
        {
            isHorizontalDown = true;
            isVerticalMovement = false;
        }

        //These two conditions allow the player to
        //continue moving in the following and similar cases:
        //"W" key pressed - movement up;
        //pressed the "D" key - movement to the right;
        //the "D" key is released, but "W" is pressed - movement up
        //___________________________________//
        if (Input.GetButtonUp("Vertical"))
        {
            isVerticalDown = false;
            if (isHorizontalDown)
                isVerticalMovement = false;
        }
        if (Input.GetButtonUp("Horizontal"))
        {
            isHorizontalDown = false;
            if (isVerticalDown)
                isVerticalMovement = true;
        }
        //___________________________________//

        if (isVerticalMovement)
        {
            movement = new Vector2(0, Input.GetAxis("Vertical"));
        }
        else
        {
            movement = new Vector2(Input.GetAxis("Horizontal"), 0);
        }
    }

By logic, when you read the script line by line, you’ll see that by pressing W key, x value will be set to zero AFTER getting value from getAxisRaw. That means that in the next if() you use, movement.x is set to 0 and will never change until you stop using W key.

What I recommend is to check if the value of movement.y or movement.x has changed. So:

public class Controls : MonoBehaviour
 {
    public Vector2 movement;
 
 void Update()
    {
         float x = Input.GetAxisRaw("Horizontal");
         float y = Input.GetAxisRaw("Vertical");
 
         float lastX; //To check if current x value has changed from last
         float lastY; //To check if current y value has changed from last
 
         string LastKeyUsed = ""; //will know which axis was changed
 
         if(x!=lastX) //if current x value has changed, we set the last used axis to X and set lastX to current x value
         {
             LastKeyUsed = "X";
             lastX = x;
         }
         if(y!=lastY) //if current y value has changed, we set the last used axis to Y and set lastY to current y value
         {
             LastKeyUsed = "Y";
             lastY = y;
         }

         if(x != 0 && y== 0) //In case you stop using the last key and have still an other key pressed, you come back to the other key axis
         {
             LastKeyUsed = "X"
         }
         if(x == 0 && y!= 0)
         {
             LastKeyUsed = "Y"
         }

 
         if(LastKeyUsed == "X") //Then we check which axis was last used, and we set movement x and y value depending on the result
         {
             movement.x = x;
             movement.y = 0;
         }
         else if(LastKeyUsed == "Y")
         {
             movement.x = 0;
             movement.y = y;
         }
    }
 }

Hope it helps, and that you understood what didn’t work.