- Home /
Animator Condition Not Working
I'm making a 2D platformer. When the space button is pressed, my character is supposed to jump. When he jumps, the trigger that plays the jump animation is set and the trigger that indicates the player is grounded/has landed resets. After the jump animation plays, it transitions to the falling animation. Once the character returns to the ground, the jump trigger resets while the grounded trigger is set, causing the falling animation to stop and return to the idle animation.
The only problem is that the falling animation ends too early. In the middle of falling back down, the idle animation starts playing, but it's only supposed to play when it is grounded. I don't have the falling animation set on an exit time and I tripled checked to make sure the falling animation is set to loop time like the idle, but it never works. The only condition for the falling animation is the grounded trigger and the only transition with an exit time is from jumping to falling.
Here's my code (only including the parts relevant to my problem): new Animator anim;
[SerializeField]
private Transform [] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jumped;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
PlayerMove ();
}
void FixedUpdate (){
isGrounded = IsGrounded();
}
void PlayerMove (){
if(Input.GetButtonDown("Jump")){
jumped = true;
if (isGrounded && jumped) {
isGrounded = false;
GetComponent<Rigidbody2D>().AddForce (Vector2.up * playerJumpPower);
anim.SetTrigger ("jump");
}
}
}
private bool IsGrounded (){
if (gameObject.GetComponent<Rigidbody2D>().velocity.y <= 0){
foreach(Transform point in groundPoints){
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0;i < colliders.Length;i++){
if(colliders[i].gameObject != gameObject){
anim.ResetTrigger ("jump");
anim.SetTrigger ("land");
return true;
}
}
}
}
return false;
anim.ResetTrigger ("land");
}
}