- Home /
How to play transitioned animation at random time
Hello I have 2 animations that transitions to each other and I want the second idle animation played at a random time between 1 & 5 seconds and then back to idle 1. But my code doesn’t seem to make that happen.
public Animator anim;
void Update()
{
anim.SetFloat(“Idle2”, Random.Range(1, 5));
}
Answer by Ymrasu · Feb 14, 2019 at 07:55 PM
What I did when I wanted a second idle animation to play (like look around the room) was to attach an animation event to the first idle animation.
So if your first idle animation is one second, put the event at the end. So the event function (on the script attached to the same object as your animations) could look something like this:
public void secondIdle()
{
// 1 in 5 chance of triggering
if (Random.Range(1,5) == 1) {
anim.SetTrigger("Idle2");
}
}
I think using a chance of it happening was easier to do, instead of a set amount of time done with an event animation. So that I don't have to check if the player remained in the idle position the whole time (up to 5 seconds). Using a chance 1 out of 5 times every second is similar to 1-5 seconds, but can go longer than 5 seconds.
I hope this helps give you an idea of what you can do if this is not the route you want.
I understand what your saying I’ll test this out & see the results thanks for the response :)