Unity: Detecting Idle Player

Ryan McCoach
3 min readNov 13, 2023

--

Intro

In this article, I will cover how to detect if the player hasn’t touch the controls after designated amount of time (seconds). This can be helpful to trigger an idle cutscene, animation or pauses the game.

My example will trigger an idle cutscene that plays until the player reengages with the controls again.

Setup

We will need the following variables:

  • idleTime (float) — this will store the amount of time that has currently passed since the last time the player interacted with the controls.
  • timeToIdle(float) — this will store the amount of idle time to wait before triggering the idle cutscene.
  • isIdle(bool) — this tracks if the idle time to wait has been reached and the cut scene is playing.

Input Check

In Update, we are constantly checking to see if the player is not interacting with the controls. This means no keys are being pressed and the mouse is not moving.

  • Input.any — this returns true if any key is being pressed.
  • Input.GetAxis(“Mouse X”) — this returns 0 if the mouse is not moving left or right.
  • Input.GetAxis(“Mouse Y”) — this returns 0 if the mouse if not moving up or down.

Using the OR operator (||), if any key is pressed and the mouse movement on the X & Y is NOT 0 this means the controls are being used.

When this is true, we set the idleTime to 0.

When it is false we add Time.deltaTime (amount of time since the last frame was rendered) to the idle time.

Idle Time Reached

Each time the idle time increases, we will check if equals/greater than the amount of time to idle and if it is NOT already idling.

If both conditions are true, then set isIdle to true. This prevents whatever you want to play or do from constantly being triggered. In my case, I play an idle cutscene using a Camera Manager.

Exit Idle

When the controls are engaged again, check if the isIdle is true. This prevents the idle cutscene being stop constantly, every time the player uses the controls.

We set isIdle to false and stop the idle scene. DON’T FORGET to set this bool to false when the script starts.

--

--