- Home /
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:
Thank you.
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 !
Yes but this starts the animation without waiting the previous one to finish.
animations have exit time . you can say after animation ends play another . does it work in your situation ?
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();
}
Your answer
Follow this Question
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