What do we need to do?
We just need to check whether the player is pressing the Escape key or not. If the escape key is pressed, then end the game. As the key press can happen at any time of the game, we need to check for it every single frame.
Implementing the logic
To check whether the Escape key was pressed or not, we need to access the Input Manager and check for the escape key. As we just want to execute the code when the key was pressed, we need an if-statement.
We want to close the application when the condition is met. As we don’t know the command triggering the closure of the application, we need to do a little research here. Simply searching for “close application unity” will reveal the command we need. Application.Quit().
Having found out the code we need, the if-statement now is:
That’s all we need! Pressing the ESC key will now close the game as soon as the player presses it.