- Home /
How to detect if the player is not moving for specific duration of time
I want my character to do some animation if it stopped moving for 30s
Answer by highpockets · Jun 02, 2019 at 11:01 PM
You should give more details here. Anybody who tried to help you here has to guess at how you are moving your character. I’ll guess that he is moved with a rigidbody, so a check to see if velocity is equal to zero can start a timer if true and once 30 seconds is reached, do the animation:
bool timerStarted = false;
float timer = 0.0f;
void Update()
{
if(rigidbody.velocity == Vector3.zero)
{
if(!timerStarted)
{
timerStarted = true;
timer = 0.0f;
}
else
{
timer += Time.deltaTime;
}
if(timer >= 30.0f)
{
anim.SetTrigger(“doALittleDance”);
timerStarted = false;
timer = 0.0f;
}
}
else
{
timerStarted = false;
}
}
That’s a bit of untested code, but it should do the trick in the rigidbody case.
Sorry for the lack of information, Anyways you guessed right Thanks ❤
Your answer
Follow this Question
Related Questions
Unity 2D - Pivot points based on slice, not on original texture. 0 Answers
Camera Effects for 2d 0 Answers
Unity 4.3, generate 2d mesh 2 Answers
2D C# Jump Script Addforce 2 Answers
Is there any way of creating primitive 2D shapes in unity? 1 Answer