The problem is that I want the order in which the keys are pressed to not matter. Right now, if I press W(up) while holding A(left), the object ignores W and keeps moving left because it’s the first in the if statement. On the other hand, if I press D(right) while holding S(down), the direction changes and the object moves right. If I let go of D(right), the object goes back to moving down; This last bit is how I want all the keys to react. How do I fix this in code? Any help would be great, thanks!
// GetKeyDOWN gets the key once when it is pressed. So, you ever get the "newest" key.
if(Input.GetKeyDown("a"))
{
moveLeftRight= - 1.0f;
moveUpDown=0.0f;
}
if(Input.GetKeyDown("d"))
{
moveLeftRight= 1.0f;
moveUpDown=0.0f;
}
if(Input.GetKeyDown("w"))
{
moveLeftRight=0.0f;
moveUpDown= - 1.0f;
}
if(Input.GetKeyDown("s"))
{
moveLeftRight=0.0f;
moveUpDown= 1.0f;
}
// Maybe reset the values.
// GetKeyUP gets the key once when it is released.
if(Input.GetKeyUp("a") && moveLeftRight== - 1.0f)
{
moveLeftRight=0.0f;
}
if(Input.GetKeyUp("d") && moveLeftRight== 1.0f)
{
moveLeftRight=0.0f;
}
if(Input.GetKeyUp("w") && moveUpDown== - 1.0f)
{
moveUpDown=0.0f;
}
if(Input.GetKeyUp("s") && moveUpDown== 1.0f)
{
moveUpDown=0.0f;
}
// (NO if)
//move left or right (X += speed * moveLeftRight);
//move up or down (Y += speed * moveUpDown);