How do I make a key activate only when another key is pressed

I’m trying to make a game where the player must alternate between 2 buttons in order to move forward. This is what I have so far.

	if (Input.GetKeyDown(KeyCode.A))
		{
			transform.position += Vector3.up * 0.025f;
		}

		if (Input.GetKeyDown(KeyCode.D))
		{
			
			transform.position += Vector3.up * 0.025f;
		}

Thanks!

There are 3 input methods to get a KeyPress.

  • Input.GetKeyDown => when Key is pressed Down

  • Input.GetKey => when Key is hold

  • Input.GetKeyUp => when Key is released

To combine two events you need to use the Operator && but this won’t work on down&&down / up&&up / up&&down because you’re not that fast to press two keys in the same frame. so you need to do something like this:

if (Input.GetKeyDown(KeyCode.A) && Input.GetKey(KeyCode.D)){}

create a variable named nextAcceptableKey and compare it when ever a key pressed/released.

Here is the snippet you need

private KeyCode nextAcceptableKey = KeyCode.A;
    void Update()
    {
        if (Input.GetKeyDown(nextAcceptableKey))
        {
            transform.position += Vector3.up * 0.025f;
            nextAcceptableKey = (nextAcceptableKey == KeyCode.A) ? KeyCode.D : KeyCode.A;//Assigns alternate keys
        }
    }