Removal of diagonal movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed;
    public KeyCode moveLeftKey = KeyCode.A;
    public KeyCode moveRightKey = KeyCode.D;
    public KeyCode moveUpKey = KeyCode.W;
    public KeyCode moveDownKey = KeyCode.S;

    void Start()
    {
        moveSpeed = 5f;
        transform.position = new Vector2(0, 0);
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = 0f;
        float verticalInput = 0f;

        if (Input.GetKey(moveLeftKey))
        {
            horizontalInput = -1f;
        }
        if (Input.GetKey(moveRightKey))
        {
            horizontalInput = 1f;
        }
        if (Input.GetKey(moveUpKey))
        {
            verticalInput = 1f;
        }
        if (Input.GetKey(moveDownKey))
        {
            verticalInput = -1f;
        }
        float newXPosition = transform.position.x + horizontalInput * moveSpeed * Time.deltaTime;
        float newYPosition = transform.position.y + verticalInput * moveSpeed * Time.deltaTime;

        transform.position = new Vector2(newXPosition, newYPosition);
    }
}

From the code above, I want to remove diagonal movement with this method of movement. How I want things to be is that I want to move in a direction in which the last key is pressed even when I’m holding keys down.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed;
    public KeyCode moveLeftKey = KeyCode.A;
    public KeyCode moveRightKey = KeyCode.D;
    public KeyCode moveUpKey = KeyCode.W;
    public KeyCode moveDownKey = KeyCode.S;

    void Start()
    {
        moveSpeed = 5f;
        transform.position = new Vector2(0, 0);
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = 0f;
        float verticalInput = 0f;
        string movedir = "";

        //if left key is pressed
        if (Input.GetKey(moveLeftKey))
        {
            if (Input.GetKey(moveUpKey))
            {
                movedir = "up";
            }
            else if (Input.GetKey(moveDownKey))
            {
                movedir = "down";
            }
            else
            {
                movedir = "left";
            }
        }
        //if right key is pressed
        if (Input.GetKey(moveRightKey))
        {
            if (Input.GetKey(moveUpKey))
            {
                movedir = "up";
            }
            else if (Input.GetKey(moveDownKey))
            {
                movedir = "down";
            }
            else
            {
                movedir = "right";
            }
        }
        //if up key is pressed
        if (Input.GetKey(moveUpKey))
        {
            if (Input.GetKey(moveRightKey))
            {
                movedir = "right";
            }
            else if (Input.GetKey(moveLeftKey))
            {
                movedir = "left";
            }
            else
            {
                movedir = "up";
            }
        }
        //if down key is pressed
        if (Input.GetKey(moveDownKey))
        {
            if (Input.GetKey(moveRightKey))
            {
                movedir = "right";
            }
            else if (Input.GetKey(moveLeftKey))
            {
                movedir = "left";
            }
            else
            {
                movedir = "down";
            }
        }

        //to check which direction its gonna move
        if (movedir == "up")
        {
            verticalInput = 1f;
        }
        else if (movedir == "left")
        {
            horizontalInput = -1f;
        }
        else if (movedir == "right")
        {
            horizontalInput = 1f;
        }
        else if (movedir == "down")
        {
            verticalInput = -1f;
        }
        //the execution of the movement
        float newXPosition = transform.position.x + horizontalInput * moveSpeed * Time.deltaTime;
        float newYPosition = transform.position.y + verticalInput * moveSpeed * Time.deltaTime;
        Debug.Log(movedir);
        transform.position = new Vector2(newXPosition, newYPosition);
    }
}     

the code above is something I’ve tried but when i press right/left then up/down it still moves right/left.

Your problem is that you have forgotten to use else if statements instead of if statements. The way you would do this is with the framework you have built is with the following code.

if (Input.GetKey(moveUpKey)) movedir = "up";
else if (Input.GetKey(moveLeftKey)) movedir = "left";
else if (Input.GetKey(moveRightKey)) movedir = "right";
else if (Input.GetKey(moveDownKey)) movedir = "down";

Although, this method would not check for the last key pressed, it would only prioritize up movement. A better way to do this would be to do:

public KeyCode lastHitKey;
// all of your other code here
if (Input.anyKeyDown) {
 lastHitKey = Event.current.keyCode;
 if(lastHitKey == moveUpKey) movedir = "up";
 else if (lastHitKey == moveDownKey) movedir = "down";
 else if (lastHitKey == moveLeftKey) movedir = "left";
 else if (lastHitKey == moveRightKey) movedir = "right";
}

I’m extremely sorry for my previous error, here is code to perform the same task as the lastHitKey, but one that allows you to keep it in the update function. Sorry for the inconvience.

if (Input.GetKeyDown(moveUpKey)) movedir = "up";
if (Input.GetKeyDown(moveLeftKey)) movedir = "left";
if (Input.GetKeyDown(moveRightKey)) movedir = "right";
if (Input.GetKeyDown(moveDownKey)) movedir = "down";