I do not want my character to move diagonally. How do I do this?

My character in my game moves in a diagonal direction, and I do not want it to. I want it to move up left down and right, based off of the last key that was pressed, but I’m not sure quite sure how to set this up:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    Rigidbody2D rbody;
    Animator anim;
    public float speed;

    // Use this for initialization
    void Start () {

        rbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void FixedUpdate () {

        Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        if (movement_vector != Vector2.zero)
        {
            anim.SetBool("iswalking", true);
            anim.SetFloat("input_x", movement_vector.x);
            anim.SetFloat("input_y", movement_vector.y);
        }
        else
        {
            anim.SetBool("iswalking", false);
        }
        speed = 1.6f;
        rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime * speed);


    }
}

You can check to see if both x and y values are non-zero in your movement vector. If that’s the case, it will move diagonally. If both aren’t 0, set one to 0 to turn it into a non-diagonal move. Or simply block the move entirely. However you want to handle it in that case.

You could have each directional key set the movement_vector to the correct direction, instead of combining all inputs from the axis inputs.

I’m not quite sure how to write that up.

I was thinking something along these lines. I haven’t tested this, but this is the general idea:

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rbody;
    Animator anim;
    public float speed = 1.6f;

    private Vector2 movementVector;

    // Use this for initialization
    void Start()
    {
        rbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            movementVector = Vector2.left;
        }
        else if(Input.GetKeyDown(KeyCode.RightArrow))
        {
            movementVector = Vector2.right;
        }
        else if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            movementVector = Vector2.up;
        }
        else if(Input.GetKeyDown(KeyCode.DownArrow))
        {
            movementVector = Vector2.down;
        }

        // if any of the movement keys were released, and the current movement vector is that direction
        if((Input.GetKeyUp(KeyCode.LeftArrow) && movementVector == Vector2.left)
            || (Input.GetKeyUp(KeyCode.RightArrow) && movementVector == Vector2.right)
            || (Input.GetKeyUp(KeyCode.UpArrow) && movementVector == Vector2.up)
            || (Input.GetKeyUp(KeyCode.DownArrow) && movementVector == Vector2.down))
        {
            // stop movement
            movementVector = Vector2.zero;
        }

        if(movementVector != Vector2.zero) {
            anim.SetBool("iswalking", true);
            anim.SetFloat("input_x", movementVector.x);
            anim.SetFloat("input_y", movementVector.y);
        } else {
            anim.SetBool("iswalking", false);
        }
    }

    // Update is called once per frame
    void FixedUpdate() {
        rbody.MovePosition(rbody.position + movementVector * Time.deltaTime * speed);
    }
}