• 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
Question by fadihasrouni · Jul 17, 2014 at 07:39 AM · animationthreads

play animation sequence

Hello,

I want to play 2 (or more) consecutive animations. For example run then roll. How can i do it using C# script? and if i want to animate more than one player in a football game with different animations should i be using threads?

Give me a helpful link for consecutive animations and threads in unity if possible. thanks in advance

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by azrijamil · Sep 20, 2014 at 10:42 AM

You can use Observable class from Reactive Extension library. For Unity use UniRx from asset store, below is some example.

     public class UIAnimationClip
     {
        public string clipName;
        public int delayAfterPlay;
     }
 
     public IObservable<bool> PlayAsObservable(List<UIAnimationClip> animClips)
     {
         return Observable.FromCoroutine<bool>(observer => this._PlayAsObservable(animClips, observer));
     }
 
     private IEnumerator _PlayAsObservable(List<UIAnimationClip> animClips, IObserver<bool> observer)
     {
         foreach (UIAnimationClip animClip in animClips)
         {
             animation.Play(animClip.clipName);
             while (animation.isPlaying)
             {
                 yield return null;
             }
             Debug.Log("Finished playing : " + animClip.clipName);
 
             if (animClip.delayAfterPlay > 0) yield return new WaitForSeconds(animClip.delayAfterPlay);
             observer.OnNext(true);
         }
 
         observer.OnNext(false);
         observer.OnCompleted();
     }

To use it, just create a sequence of observable collection, this example is not optimize and it just for the example purposes.

 void Start()
 {
     this.animationClips.Add(new UIAnimationClip{
         clipName = "fade-in",
         delayAfterPlay = 2
     });
 
     this.animationClips.Add(new UIAnimationClip{
         clipName = "fade-out",
         delayAfterPlay = 0
     });
 
     this.PlayAsObservable(this.animationClips).Where(p => p == false).Subscribe(stop =>
     {
         Debug.Log("Complete");
     });
 }

It will push true value after each play, and false if no more animation clip to play.

Comment
wontheone1
nonopblic

People who like this

2 Show 0 · 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

Answer by Saad_Khawaja · Jul 17, 2014 at 08:29 AM

You can use Animator Controller (Mecanim) on a GameObject for Animation. Mecanim uses state machines to create transitions between different animations.

For e.g. If you wanted to play Roll after Run, you could create two states - (the first being run and the second roll and attach the respective animations)

I would advise you to go through the Unity Mecanim tutorials here: http://unity3d.com/learn/tutorials/modules/beginner/animation

Comment

People who like this

0 Show 1 · 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 fadihasrouni · Jul 17, 2014 at 08:34 AM 0
Share

What if i have like almost 25 animations and i have to switch between 2 of them? for example i have those animations: run-roll-jump-kneel...! So at first i want to play run and roll and after like 2 seconds i want to play run and jump and then after 2 other i want to run roll then kneel. How can i do it with c# script using the animator controler?

avatar image

Answer by Rigo.cl · Jul 17, 2014 at 11:25 PM

I suggest you use Coroutines (http://docs.unity3d.com/Manual/Coroutines.html) combined with animation["anim"].length, Coroutines are basic and very useful threads, something like this:

In your Update method:

 if (!isPlaying)  // nothing playing? let's play something...
 {
     string nameAnim;

     // now assign 'animId' some random int between 1 and 25.

     switch (animId)
     {
     case 1:
         nameAnim = "anim1"; break;
     case 2:
         nameAnim = "anim2"; break;
     case 3:
         nameAnim = "anim3"; break;

     case 25:
         nameAnim = "anim25"; break;
     }

     // now an animation is running
     isPlaying = true;

     // now wait for the anim to finish plus 2 seconds more.
     StartCoroutine(LetsWait(animation[nameAnim].length + 2));
 }


Your Coroutine:

 IEnumerator LetsWait(float secsToWait)
 {
     yield return new WaitForSeconds(secsToWait);  // we wait here (not exactly actually)...

     // done waiting... now update the playing flag to go for the next animation.
     isPlaying = false;
 }

If you don't want to wait always 2 seconds more, then create a float and inside every 'case' assign the additional seconds to wait.

Pretty much that's it.

regards.

Comment

People who like this

0 Show 1 · 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 Huacanacha · Jul 17, 2014 at 11:38 PM 0
Share

Coroutines are not threads, even if they share some basic properties. They're more akin to the old cooperative multitasking model (taking turns running in sequence, passing control to the next by Yielding) than true threading (multiple processors and/or time splicing processor resources).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation Problems 1 Answer

Character controller problem? 2 Answers

Simple Sprint? 2 Answers

A node in a childnode? 1 Answer

Quick animation question 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