Reputation: 11
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
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
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
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