My solution for the Only one button pressing at the same time challenge

2021-06-25T05:00:00Z

So, I tried doing the same as Rick,with the else if structure, but it didn’t result.

Finally I found out that I could use a conjunction using AND.

What I’m saying here is basically this, let’s take for example the first if statement:
If the Up Arrow Key is pressed AND the Down Arrow Key is NOT pressed AND the Return key is also NOT pressed, then execute this code block:
{ Debug.Log(“Up Arrow Key Pressed”) }

Note that in the first proposition I’m using Input.GetKeyDown, whereas on the one that comes after the “&&” I’m using Input.GetKey, the difference between these two is that Get Key Down checks if a key was pressed only during the exact frame that you press that key. While Get Key keeps checking every frame that you hold that key, so when you press all the keys at the same time it only reads the one that you pressed first.

Place the code inside your void Update()

        if (Input.GetKeyDown(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.Return))
        {
            Debug.Log("Up Arrow Key Pressed");
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.Return))
        {
            Debug.Log("Down Arrow Key Pressed");
        }
        else if (Input.GetKeyDown(KeyCode.Return) && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
        {
            Debug.Log("Return Key Pressed");
        }
3 Likes

Awesome job.

Privacy & Terms