Animation in C#. Waiting for an animation to end, stuck in an animation.

I’m working on coding in some animation for a project. I need a jumping animation to play once the camera moves to a certain height, then a falling animation to play after that animation and loop until I activate a grabbing animation after a touch input. The later stuff shouldn’t be that difficult but I’m having trouble with getting the first two to work together.

public class TouchScreenTests_Player_Redux : TouchScreenTests_Core {
//Inherits from the TouchScreenTests_Core script instead of MonoBehavior


	//Detects whether the character is on the first jump of the level or not.
	public bool levelStart;

	//Detects whether the character is falling or not.
	private bool falling;

	//Sets up touchBegan and touchEnd values
	private Vector2 touchBegin, touchEnd;

	private bool touchBegan;

	//Creates variables for the characters transform and the cameras transform.
	private Transform myTransform, camTransform;

	//Sets the characters movement speed for horizontal movement
	public float speed = 15.0f;


	//Sets a check for whether the jump animation has played or not.
	public bool jumpHasPlayed = false;

	//Sets a check for whther the grabbing animation has played or not.
	public bool grabHasPlayed = false;
	

	public GameObject anchor;

	public GameObject Thief;
	
	private Vector2 screenPos;
	
	public bool paused;

	void Start () 
	{
		//Sets the LevelStart value to true, only once.
		levelStart = true;

		//Sets the falling value to false
		falling = false;

		Thief = GameObject.FindGameObjectWithTag ("Thief");

		//Turns off gravity at the start of the level for this rigidbody.
		//this.rigidbody.useGravity = false;

		//Sets myTransform to the transform to which this code is assigned.
		myTransform = this.transform;

		//Sets camTransform to the transform of the main camera
		camTransform = Camera.main.transform;

	}
	
	void Update () 
	{
	
		TouchCheck();
		
			if (camTransform.position.y >= 180.0f && levelStart == true)
			{
				Thief.animation.Play ("Jumping");

				jumpHasPlayed = true;

				levelStart = false;

				Debug.Log ("Jump has Played");
			}
			
			if (!Thief.animation.isPlaying && camTransform.position.y <= 180.0f && levelStart == false);
			{
				Thief.animation.Play ("Falling");
			}

The problem I’ve run into is that the “Falling” animation is overiding the Jumping animation and I can’t cause any other animation to play once it does (I’ve got a secondary code just to test things that plays animation after button input). the “Jump has Played” Debug happens but the actual jump animation does not play. Currently the Camera starts out at around 200 so the Jump animation should play first, and because an animation is playing Falling shouldn’t play until that is done. Although, the Thief.animation.isPlaying is something I guessed on the spot in hopes of it working because this code is not on the object being animated so animation.isPlaying doesn’t work, it tries to find an animation on the object this code is attached to.

I’m looking for a way to basically tier the animation priority or anything like that. Also, a way to detect whether a specific animation has ended so I can move the jumpHasPlayed and levelStart variables into a statement with that so they don’t change until the animation actually finishes. I’ve looked through PlayQueued on the Script reference but I’m not really getting it so any explanation on that may help as well…

You could do for when the animation is not playing:

while(!animation.isPlaying ){

DoSomething();

}

or when it is playing:

while(animation.isPlaying){

DoSomething();

}

and for custom animations just add:

    while(animation["theif animation name"].enabled == false){
        //so if you enter true it means while the animation is playing and false while it is not
        // you can change while loop == to true if need be
        DoSomething();
    }

1st option is to use mecanim, there you can sequence animation and specify conditions in unity 5 there will be some extended functionality you might use

2nd is to write your small system for it and bit change the conditions

I generally don’t use while loops that much, but I definitely wouldn’t use them for animations since you can easily just do the logic checks within the Update() using a while would be excessive and could potentially break things. Since Update() occurs once every frame and your animations have no reason to be doing stuff more than once per frame I think while loops are not a good way to do this.

I think you just need to rearrange your logic a little bit with if and else if conditions.

I’ll take a crack at a quick example based on your original code:

if (camTransform.position.y >= 180)
{
Thief.animation.Play ("Jumping");
}

else if (camTransform.position < 180)
{

//need some logic to determine
//if player is using the grab animation
//the grab logic should go BEFORE the fall
//because it will always override it right?
//for example:
if (grabBool == true)
{
Thief.animation.Play ("Grab");
}

//use else if so that if grab is playing
//you can't fall
else if (grabBool == false)
{
Thief.animation.Play ("Falling");
}
}

A lot of your other logic seems unnecessary for the actual animations since the only things that matter from my understanding are:

  1. If you are greater than or equal to 180 meters you jump

  2. If you are less than 180 meters you either:

a)grab animation

b)if you are not grabbing you use fall animation

I think that’s the most basic breakdown of the animation logic. If your other bool variables are necessary for other aspects of your game you can still include them but I don’t think they are necessary for the animations, only the height and if they are grabbing or not. Hope that helps!

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

Toko bunga bandung