- Home /
Question by
gabrielemuratori · Jan 16, 2020 at 08:06 PM ·
2d2d gameplatformer2d animation
Stop jump animation
How do I stop my jump animation after my player has reached the ground?
public class CharacterController : MonoBehaviour
{
public float moveSpeed = 5f;
public bool isGrounded = false;
public Sprite player;
float horizontalMove;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),0f,0f);
transform.position += movement * Time.deltaTime * moveSpeed;
anim.SetFloat("Speed", Mathf.Abs(0f)); //This is where I'm trying to change thee variable to stop the animation, but this part is not working
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
anim.SetFloat("Speed", Mathf.Abs(5f)); //Here I'm changing the variable so that the animation starts
}
}
}
Comment
Answer by oboy_gamer · Jan 19, 2020 at 09:55 AM
You are resetting the value every frame. The animation doesn't get a chance to start playing. Instead, add an if statement.
if (isGrounded){
anim.setFloat("Speed", 0f);
}