- Home /
Play 2D animations via trigger parameters
I'm starting with Unity and exploring the 2D Platformer example project to learn some things. I'm already porting it to Windows Phone(just to get experience).
I made GUI buttons and Rects to control the hero. All the buttons are working fine, but the move left and move right buttons dont play the "Run" animation. Looking at the scripts I can see that they use triggers to run animations, like anim.SetTrigger("Jump");
, so I opened the Animation Controller named character and added a new Trigger to the parameters called "Run", on the scripts I added the following code to the buttons script: anim.SetTrigger("Run");
.
The buttons are working fine, but they dont play the run animation, it looks like the hero is floating on the ground.
Here is the buttons code:
if (GUI.RepeatButton(new Rect(250,Screen.height - 200,200,200), rightIcon, framestyle))
{
float h = 1;
anim.SetFloat("Speed", Mathf.Abs(h));
if(h * rigidbody2D.velocity.x < maxSpeed){
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
//Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
}
if (GUI.RepeatButton(new Rect (50,Screen.height - 200,200,200), leftIcon, framestyle))
{
float h = -1;
anim.SetFloat("Speed", Mathf.Abs(h));
if(h * rigidbody2D.velocity.x < maxSpeed){
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
//Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
}
It's inside the OnGUI() of the PlayerController and I defined all the variables. I think that I need to attach the "Run" trigger to the "Run" animation, but I don't know how.
Someone can help me?
Answer by gfoot · Jan 20, 2014 at 05:04 AM
In the Animator window, with the game object selected, you'll see nodes for all the clips the character can play, with arrows between the nodes showing the possible transitions. You probably need to hook your parameter up to some of the transitions.
Just click on a transition arrow, then in the inspector you'll see a "Conditions" box that defines the conditions that cause the transition. The default is based on Exit Time, meaning it transitions when the previous animation has nearly finished. Change that to be based on your new parameter (which should appear in the drop-down list automatically if you added it correctly).
Or, if there are transitions missing, add them by right-clicking on the state to transition from, and using the menu option there.
Running your game with the Animator window visible can help to diagnose state change errors.
Thanks man. :) ' With that I figured out that the "Run" animation is triggered when the float parameter "Speed" is higher than 0, so I fixed the buttons code.
Another noob question: how do I close the game on Windows Phone through the scripts? (like an exit button)
Cool. I don't know Windows Phone but generally you call Application.Quit() to quit your app.