using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour
{
public string jumpButton = "Fire1";
public float jumpPower = 10.0f;
public Animator anim;
public bool grounded = false;
public float minJumpDelay = 0.5f;
public Transform groundCheck;
private float jumpTime = 0.0f;
private bool jumped = false;
// Use this for initialization
void Start ()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
jumpTime -= Time.deltaTime;
if(Input.GetButtonDown(jumpButton) && grounded)
{
jumped = true;
grounded = false;
anim.SetTrigger("Jump");
rigidbody2D.AddForce(transform.up*jumpPower);
jumpTime = minJumpDelay;
}
if(grounded && jumpTime <= 0 && jumped)
{
jumped = false;
anim.SetTrigger("Land");
}
if(grounded == false && jumped == false)
{
anim.SetTrigger("Fall");
}
if(grounded)
{
anim.SetTrigger("Fall_Land");
}
}
}
This section here controls when to play my falling animation on my 2D game (unity 4.3) however when my character is falling its all ok but when a land or fall from a ledge it repeats the transition to the fall twice. So it goes from walking animation to falling to walking to falling in the space of a few frames.
if(grounded == false && jumped == false)
{
anim.SetTrigger("Fall");
}
if(grounded)
{
anim.SetTrigger("Fall_Land");
}