How to create a blinking eye animation with 2 images

I am a bit new here, I would like to know how to create a blinking eye animation that changes between an open eye image for 3 to 5 seconds to a closed eye image for 0.5 seconds using an animation controller? I would appreciate some help.

Hey,
First you add an animator to your character:

Then in your project assets you create a new Animator Controller:

You drag and drop it to its slot:

Double click on the Animator Controller, it will open a new tab, now create two animations in the same way we created the animator controller:

add each srpite to each animation (the open eye to one and the closed eye to the other one)
and drag and drop them them to the animator controller window.
You should be seeing something like this:

Now we will make our loops:

1 Like

Right click on the animation and left click on make transistion:
2636695--185488--upload_2016-5-16_12-10-31.png

Match them in both ways

Now double click the close to open arrow, in the inspector some settings will appear, modify the exit time to 0.5s

Now the same for the open to close arrow and set the exit time to 3s, there you have :slight_smile:


1 Like

I am trying to follow your instructions but I don’t get two things:

  1. How do I add the a sprite to an animation?

“add each srpite to each animation (the open eye to one and the closed eye to the other one)”
2. How do I set the Exit Time for a range 3 - 5 seconds (meaning, 3, 4 or 5 seconds as Samgil stated) instead of keeping it fixed?

-Double click the animation and drag and drop the sprite :slight_smile:

  • I can imagine 2 approaches to this, a blend tree or scripting it, I would go for the script (easier for me)

Something like this should do the trick:

    private Animator anim;
    void Start () {
        anim = GetComponent<Animator>();
        StartCoroutine(BlinkManager());
    }
    public bool blinking;
    private IEnumerator BlinkManager()
    {
        blinking = true;
        while (blinking)
        {
            int blinkTime = Random.Range(3, 5);
           anim.Play("Open");
            yield return new WaitForSeconds(blinkTime);
             anim.Play("Close");
            yield return new WaitForSeconds(0.5f);

        }

    }

Oh, if you go for the scripting you have to remove all the transitions in the animator

Thanks, works perfectly, though for curiosity sake I would like to know how to achieve the same result with the inspector :).
More importantly, what I just did was a simple case. What happens to the animation when my game object is a prefab? Say I have a ball prefab with colors green, blue and red, with sprite images greenBall01 (open), greenBall02 (closed) and so on respectively. How will I handle this?