Anthony Praise Ofume
Anthony Praise Ofume

Reputation: 11

How to detect when the Player is not playing the game

I am making a game in Unity and I want to check if the game's player is clicking anything or not. So if the player is idle for certain amount of time, lets say 20 seconds, then I want to play an animation to indicate that the player is idle.

Upvotes: 0

Views: 2699

Answers (3)

Ian H.
Ian H.

Reputation: 3919

If you want to create an entirely new script for this purpose ...

public static class IdleCheck
{
    public static int Timeout { get; set; }

    private static float lastAction;
    public static void ReportAction ()
    {
        lastAction = Time.time;
    }

    public static bool IsIdle
    {
        get { return (Time.time - time) > Timeout; }
    }
}

Every time the user reports an action, like clicking, simply call IdleCheck.ReportAction() and use IdleCheck.IsIdle wherever needed.

Upvotes: 1

Chopi
Chopi

Reputation: 1143

I think something like this should do it

anyClick should be true when you get a click.

float timeSinceLastClick = 0.0f;
void Update()
{
    timeSinceLastClick += Time.deltaTime;

    if(anyClick)
    {
        timeSinceLastClick = 0.0f;
        anyClick = false;
    }

    if(timeSinceLastClick > 20.0f)
    {
        //Play Idle animation
    }
}

Upvotes: 0

Nikita Chernykh
Nikita Chernykh

Reputation: 270

You should try to use events like e.mousePosition to see if it was moved and have a counter set up for 20 sec. If event e.mousePosition not triggered in 20 sec then play the idle animation. refer to Unity documentation here: https://docs.unity3d.com/ScriptReference/Event-mousePosition.html

Upvotes: 0

Related Questions