How do i make character eyes blink after some seconds have passed? (on 2D sprite)
I'm trying to make my character blink randomly after 4-10 seconds. for the eyes(i set it as a child object, under the main character body sprite), i made in the animator two states: one idle eye state and another that has the closing eyes animation, my question here is how do i make it so the transition condition between "idle eye" and "closing eye" is like "do this after 4-10 seconds" i thought that i could do this using a script, but i don't exactly know how... i'm new to unity and have little coding experience (in C#). if there's a better way of doing this, please tell.
an idea of how i want it:
idle eye -> (after 4-10 random chosen seconds) -> do the closing eye animation -> back to idle eye -> (loop)
here's how my animator looks to get a better idea:
Answer by Zoelovezle · Nov 17, 2015 at 05:07 AM
// Random eye blinker script
public float blinkEyeRate ;
private float previousBlinkEyeRate ;
private float blinkEyeTime ;
void Update()
{
if(Time.time > blinkEyeTime)
{
previousBlinkEyeRate = blinkEyeRate ;
blinkEyeTime = Time.time + blinkEyeRate ;
//set a trigger named "blink" in ur animator window and then set that trigger the arrow connectiing eyeIdle to eyeBlink
_animator.SetTrigger("blink") ;
while(previousBlinkEyeRate = blinkEyeRate )
{
// Random Rate from 4 secs to 10secs
blinkEyeRate = Random.Range(4f , 10f ) ;
}
}//and there u get
}
thanks it works! i added some stuff though:
// Random eye blinker script
public float blinkEyeRate;
private Animator anim; //ADDED THIS
private float previousBlinkEyeRate;
private float blinkEyeTime;
void Update()
{
anim = gameObject.GetComponent<Animator>(); //ADDED THIS
if (Time.time > blinkEyeTime)
{
previousBlinkEyeRate = blinkEyeRate;
blinkEyeTime = Time.time + blinkEyeRate;
//set a trigger named "blink" in ur animator window and then set that trigger the arrow connectiing eyeIdle to eyeBlink
anim.SetTrigger("blink");
while (previousBlinkEyeRate == blinkEyeRate) //CHANGED THE "=" TO "=="
{
// Random Rate from 4 secs to 10secs
blinkEyeRate = Random.Range(4f, 10f);
}
}//and there u get
}
Oh nice :) I guess i forgot :P but grt job :) i made some mistakes LOL