Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Undyinglag · Nov 17, 2015 at 01:32 AM · unity 5sprite2d gameanimatorunity 2d

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: alt text

helpeyeblinking.jpg (167.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
     }
















Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Undyinglag · Nov 17, 2015 at 05:17 PM 1
Share

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
     }

avatar image Zoelovezle Undyinglag · Nov 19, 2015 at 06:41 AM 0
Share

Oh nice :) I guess i forgot :P but grt job :) i made some mistakes LOL

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 0 Answers

top down 2d shooter direction of enemies 0 Answers

How do I turn off the previous object? 1 Answer

Performance on 2D only: Animator.Update 1 Answer

Sprite Bug when move 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges