Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create a blinking eye animation with 2 images

Discussion in '2D' started by Samgil, May 16, 2016.

  1. Samgil

    Samgil

    Joined:
    May 16, 2016
    Posts:
    1
    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.
     
  2. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Hey,
    First you add an animator to your character:
    upload_2016-5-16_11-59-3.png

    Then in your project assets you create a new Animator Controller:
    upload_2016-5-16_12-1-20.png

    You drag and drop it to its slot:
    upload_2016-5-16_12-3-25.png

    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:
    upload_2016-5-16_12-6-3.png

    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:
    upload_2016-5-16_12-8-39.png

    Now we will make our loops:
     
    theANMATOR2b likes this.
  3. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Right click on the animation and left click on make transistion:
    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
    upload_2016-5-16_12-15-15.png

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

    Attached Files:

    theANMATOR2b likes this.
  4. fabbicular

    fabbicular

    Joined:
    May 21, 2016
    Posts:
    4
    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?
     
  5. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    -Double click the animation and drag and drop the sprite :)

    - 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:
    Code (CSharp):
    1.  
    2.     private Animator anim;
    3.     void Start () {
    4.         anim = GetComponent<Animator>();
    5.         StartCoroutine(BlinkManager());
    6.     }
    7.     public bool blinking;
    8.     private IEnumerator BlinkManager()
    9.     {
    10.         blinking = true;
    11.         while (blinking)
    12.         {
    13.             int blinkTime = Random.Range(3, 5);
    14.            anim.Play("Open");
    15.             yield return new WaitForSeconds(blinkTime);
    16.              anim.Play("Close");
    17.             yield return new WaitForSeconds(0.5f);
    18.  
    19.         }
    20.  
    21.     }
    Oh, if you go for the scripting you have to remove all the transitions in the animator
     
  6. fabbicular

    fabbicular

    Joined:
    May 21, 2016
    Posts:
    4
    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?