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
1
Question by paulaceccon · Jan 07, 2015 at 03:57 PM · animationanimator

Playing one animation after another through Animator Controller

Hi, I have a prefab which has a Animator component and what I'm trying to do is to play one animation after another one.

Specifically, my character walk from point A to point B and, according to a condition, he has to stop and another animation has to play before he starts walking again.

This is my code so far:

 public class PlayerStreetCharacter : MonoBehaviour {
 
     [SerializeField]
     private float _velocity;
 
     [SerializeField]
     private Transform _baker;
 
     private Animator _animator;
     private SpriteRenderer _spriteRenderer;
 
     private bool _isSelling;
     private int _state; /** 0 (idle), 1 (walking), 2 (restless) **/
 
     private Vector3 _startPosition;
     private Vector3 _endPosition;
 
     private void Awake()
     {
         _animator = GetComponentInChildren<Animator>();
         _spriteRenderer = GetComponentInChildren<SpriteRenderer>();
         
         GameObject waypointsList = GameObject.Find( "BakerWaypoints" ).GetComponent<WaypointManager>().GetStallWaypoints( StreetList.Instance.CurrentStreet.ID - 1); 
         Transform[] waypoints = waypointsList.GetComponentsInChildren<Transform>();
         _startPosition = waypoints[1].position;
         _endPosition = waypoints[2].position;
 
         _baker.position = _startPosition;
     }
 
     private void Update()
     {
         Walk();
     }
 
     private void Walk()
     {
         _spriteRenderer.sortingOrder = (int) (-transform.position.z);
 
         transform.position = Vector3.MoveTowards(transform.position, _endPosition, _velocity * Time.fixedDeltaTime);
         
         WalkingDirection( _endPosition );
         
         if ( ( _endPosition - transform.position ).magnitude <= 1 )
         {
             Vector3 temp = _startPosition;
             _startPosition = _endPosition;
             _endPosition = temp;
 
             CheckingSells();
             
             if( !_isSelling )
             {
                 StartCoroutine( Restless() );
             }
         }
         else 
         {
             _animator.SetInteger("State", 1);        
         }
     }
 
     IEnumerator Restless() 
     {
         _animator.SetInteger("State", 2);
         yield return new WaitForSeconds(2f);
     }
 
     private void WalkingDirection( Vector3 position )
     {
         Vector3 dir = ( position - transform.position ).normalized;
         if( Mathf.Sign( transform.localScale.x ) != Mathf.Sign( dir.x ) )
         {
             Flip();
         }
     }
     
     private void Flip()
     {
         Vector3 scale = transform.localScale;
         scale.x *= -1;
         transform.localScale = scale;
     }
 
     private void CheckingSells()
     {
         GameObject clientWaypoints = GameObject.Find( "ClientWaypoints" ).GetComponent<WaypointManager>().GetStallWaypoints( StreetList.Instance.CurrentStreet.ID - 1 ); 
         Transform[] waypoints = clientWaypoints.GetComponentsInChildren<Transform>();
 
         _isSelling = true;
         for (int i = 0; i < waypoints.Length; i++)
         {
             Waypoint waypoint = waypoints[i].GetComponent<Waypoint>();
             if ( waypoint != null && waypoint.IsEmpty )
             {
                 _isSelling = false;
             }
         }
     }

The problem is that I can't see the Restless animation be played anyway. I would like to start an animation just when the other one finish. But using the Animatior component. I don't how is the correct approach to do that. I've been searching and didn't find a solution that works for me.

This is how my character is defined:

alt text

alt text

Thank you.

captura de tela 2015-01-07 às 13.52.21.png (52.9 kB)
captura de tela 2015-01-07 às 13.52.36.png (62.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Qasem2014 · Jan 07, 2015 at 06:19 PM

you have parameter right ? you can change it with code !

 GetComponent.<Animator>().SetBool("someparameter",true);

you can go to specific animation with this conditions !

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 paulaceccon · Jan 07, 2015 at 06:43 PM 0
Share

Yes but this starts the animation without waiting the previous one to finish.

avatar image Qasem2014 · Jan 07, 2015 at 07:03 PM 0
Share

animations have exit time . you can say after animation ends play another . does it work in your situation ?

avatar image
0

Answer by Meena_Fari · Oct 27, 2016 at 07:01 AM

yet, there is no function like, isPlaying() etc. for animator, but any how you can check that state is occupied or leaved, this way your problem can be solved.

try this (0) is your layer number

     if (this.anim.GetCurrentAnimatorStateInfo (0).IsTag ("your state tag") == true) 
 {
 temp = true;
      }

       //after some time,

       if (temp && this.anim.GetCurrentAnimatorStateInfo (0).IsTag ("your state tag") == false) 
    {
     temp = false;
     //do stuff here, like call next state, through anim.SetBool();
     }
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

27 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

Related Questions

2D Animation does not start 1 Answer

Why does my animation only play the first frame and just for a small fraction of a second? 2 Answers

Triggering a Player Hit Animation 2 Answers

Attaching a child object to a parent object that animates it in runtime 0 Answers

Method Animator.SetBool sometimes is not working (or not triggering) in Unity2D 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