Idle counter.

Hello,

I am trying to make an idle counter that triggers an action if no user keyboard or mouse input has been detected for a certain amount of time. If there is user input before the timer runs out, I would like the timer to reset without performing the action.

I did a search and found a script but unfortunately for me, it does not reset the counter if there is user input before the timer runs out.

Any ideas?
Thanks.

using UnityEngine;
using System.Collections;

public class ParticleColor : MonoBehaviour {

    public bool idle = false;
    public float timeToIdle = 2.0f;  // 2 seconds
    float currentTime = 0f;
       
    void Start () {
        currentTime = Time.time + timeToIdle;
    }

    void Update () {
       
        if(Input.anyKey == false )
        {
            checkIdle();
        }
    }
   
    void checkIdle()
    {
        if(Time.time > currentTime)
        {
            idle = true;

            // perform action

            this.GetComponent<ParticleSystem>().startColor = new Color(Random.value, Random.value, Random.value);
            print("No input, change color");

            currentTime = Time.time + timeToIdle;
        }

    }
}

you just need an else clause for the if in update which has the exact same line of code as in the start function…

1 Like

Yep, solved it!
Thank you!