Stop diagonal movement 3D

Hey, I am trying to find a way to eliminate the diagonal movement from this code but can’t get it to work, it would be great if someone could provide a solution, thanks :slight_smile:

        CurrentInput = new Vector2((isSprinting ? SprintSpeed : isCrouching ? CrouchSpeed : WalkSpeed) * Input.GetAxis("Vertical"), (isSprinting ? SprintSpeed : isCrouching ? CrouchSpeed : WalkSpeed) * Input.GetAxis("Horizontal"));

        float MoveDirectionY = MoveDirection.y;
        MoveDirection = (transform.TransformDirection(Vector3.forward) * CurrentInput.x) + (transform.TransformDirection (Vector3.right) * CurrentInput.y);
        MoveDirection.y = MoveDirectionY;

FIrst of all, you have to decide what happens if a user presses both up and right together, for example. Do they stay still or does one of the directions take priority?

Let’s say that you wanted up/down to take priority. Whenever you see "Input.GetAxis(“Horizontal”) is not zero, check if vertical is pressed and only if it isn’t then move horizontally.

Your code for calculating CurrentInput is quite complex, with multiple ternary operators. I would recommend splitting that up so you can do your horizontal/vertical tests. Code with multiple embedded ternary conditions is difficult to maintain.

Always remember that you only write the final code once but read it many, many times so legibility is far more important than conciseness.