# Dynamic 4 Directional Movement

Greetings!
I’m working on a top-down grid based game with simple 4 directional movement system.

The issue I’m having is that I cannot have my character move a certain direction while holding another.
In other words; I’m trying to get my character to move A or D while holding W or vise versa.

I know that the issue lies with the dependency order of the if statements for the inputs, but I’m not sure how to go around it. I’ve tried many things (bools, ifs within ifs within ifs, && || variations etc) and I cannot find anything about it online that translate to unity.

The code consist of simple
if (Input.GetKey(KeyCode.W))
else if (Input.GetKey(KeyCode.S))
else if (etc…)

Also tried using GetAxisRaw(“Horizontal/Vertical”) with different implementations but its the same.

Please share your thoughts!

Well… It’s not pretty, but got it working using GetKeyUp + GetKeyDown and some bools.
Looks a bit like this for those curious:

``````        axis.x = Input.GetAxisRaw("Horizontal");
axis.y = Input.GetAxisRaw("Vertical");
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S))
allowHorizontal = false;
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
allowVertical = false;
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
allowHorizontal = true;
else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
allowVertical = true;

if(allowMove)
{
if(axis.x != 0 || axis.y != 0)
{
orderToMove(); //allowMove = false
if(axis.x != 0 && allowHorizontal) {
move.dir_xyz = new float3(axis.x, 0, 0);
allowVertical = true;
}
else if (axis.y != 0 && allowVertical) {
move.dir_xyz = new float3(0, axis.y, 0);
allowHorizontal = true;
if (axis.x != 0 || axis.y != 0)
allowHorizontal = false;
}
}
}
``````

Must be a better way of writing it, but since it works, i’ll just leave it for now…