My player moves diagonally when you press two keys at once
The player in my RPG I was starting to develop moves diagonally when you press two arrow keys at once. I thought it would be easy to stop this movement but, I could not do it. Here is what I tried: public class Move: MonoBehaviour {
public Transform playerPos;
public Animator Ani;
public float moveSpeedX;
public float moveSpeedY;
private bool MovingX;
private bool MovingY;
private Vector2 lastMove;
private void Start()
{
Ani = GetComponent<Animator>();
}
void FixedUpdate () {
MovingX = false;
MovingY = false;
moveSpeedX = 8.5f;
moveSpeedY = 8.5f;
if (Input.GetAxis ("Horizontal") > 0.4f || Input.GetAxis ("Horizontal") < -0.4f && MovingY == false) {
lastMove = new Vector2(Input.GetAxis("Horizontal"), 0f);
MovingX = true;
playerPos.Translate(new Vector3(Input.GetAxis("Horizontal") * moveSpeedX * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxis ("Vertical") > 0.4f || Input.GetAxis ("Vertical") < -0.4f && MovingX == false) {
lastMove = new Vector2 (0f, Input.GetAxis ("Vertical"));
MovingY = true;
playerPos.Translate (new Vector3 (0f, Input.GetAxis ("Vertical") * moveSpeedY * Time.deltaTime, 0f));
}
Ani.SetFloat("X", Input.GetAxis("Horizontal"));
Ani.SetFloat("Y", Input.GetAxis("Vertical"));
Ani.SetBool("Moving",MovingX == true || MovingY == true ? true:false);
Ani.SetFloat("LastX", lastMove.x);
Ani.SetFloat("LastY", lastMove.y);
}
}
I changed my code so there was two Moving variables. Originally there was only a Moving variable but, I created MovingX and MovingY. I made it so the player can only move left and right when MovingY is false and vice versa. I though this would fix the issue but, I can still move diagonally when I am going up and press right arrow but, when I move down and hold left I continue to move down. Can someone please tell what I have done wrong and I can get the player to continue moving in the direction he originally was when another arrow key is pressed.