Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by will_brett · Mar 30, 2014 at 11:37 PM · animationwait

Wait Random time then play animation

Hey everyone,

Hoping this is a simple one but its got me stumped.

Im making a simple 2d games and I want my characters to blink occasionally. So I need to create a sprite animation and then have the animator wait a random amount of time and then play the blink animation.

What I've tried so far is making a blink animation and then created this script to play the animation however this did not work

 public class WaitAndPlayAnim : MonoBehaviour {
 
     public float randomWait;
 
     void Start()
     { 
         StartCoroutine ("WaitSeconds"); //wait random seconds for animation
     }
     
     IEnumerator WaitSeconds () 
     {
         while(true) 
         {
             var randomWait = Random.Range(0, 6);
             Debug.Log ("wait " + randomWait + " Seconds"); 
             animation.Play("Blink");
             yield return new WaitForSeconds(randomWait);
 
         }           
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Sanky · May 27, 2015 at 07:25 AM

He try this code its work for me... Comment if occurred any issue

bool playAnim = true; // var for boolean

  void Update(){
      if (playAnim)
          StartCoroutine(WaitAnim()); //wait random seconds for animation
  }
  
  public IEnumerator WaitAnim()
  {
      playAnim = false;              
      int randomWait = Random.Range(0, 10);                
      print ("Time" + randomWait + " Play"); //debug                
      yield return new WaitForSeconds(randomWait);                
       animation.Play("Blink");;  //Put your animation string
      playAnim = true;             
  }
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 johnCpr · Feb 12, 2017 at 10:47 PM 0
Share

Thank you Sanky

avatar image modish10 · May 28, 2017 at 09:11 PM 0
Share

This helped . Thanks .

avatar image
0

Answer by NCartist · Apr 07, 2018 at 11:42 PM

I had to modify this code to get it to work in Unity 2017. This code worked for me.

  public IEnumerator WaitAnim()
     {
         playAnim = false;
         int randomWait = Random.Range(0, 5);
         print("Time" + randomWait + " Play"); //debug                
         yield return new WaitForSeconds(randomWait);
         anim.Play("Blink", -1, 0f);  //Put your animation string
         playAnim = true;
     }
Comment
Add comment · 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

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

25 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

Related Questions

Multiple Cars not working 1 Answer

I need help with adding my character controller to a Character model 2 Answers

How to get gun to move with models hand? 1 Answer

i get weird rotations on my animated objects using legacy, help? 0 Answers

please help looking for animater for FPS 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