Last key pressed

Hey guys^^

I wanted rebuild my 4 way, 2D shooting script, so that it shoots only in the direction where the last key is pressed.

This is what I have so far:

       if (Input.GetKey ("down")) {
			shotDirection = 1;
		}if (Input.GetKey ("up")) {
			shotDirection = 2;
		}if (Input.GetKey ("left")) {
			shotDirection = 3;
		}if (Input.GetKey ("right")) {
			shotDirection = 4;
		}

But with this way, if more keys pressed it takes the last key from the order.
I expermented with Event.current.key only to see that Event works only in OnGUI xD

Sadly I have no idea how to solve this :frowning:

Does anyone have some suggestions for dealing with this problem?

Thanks a lot guys :slight_smile:

GetKey will return true every frame the key is down. If you use GetKeyDown then it will only return true the frame you first press the key. Try using that instead.

It will still take the last key from the order if all statements are true in the same frame, but it will only happen if you press them all at once. Unfortunately I think you need to prioritize directions in this case.