Answer the question
In order to leave comments, you need to log in
Unity animation delay?
There is an object with three animations: rest, run, jump.
The character's legs have a collider that checks whether the character is on the ground or not. If the character is not on the ground, a jump animation is played. Everything works great here.
Rest is always playing when no other animations are playing. This is the main animation. Running is played when the value of the Speed
variable becomes greater than 0.01, rest when it is less than 0.01.
Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnim : MonoBehaviour
{
Rigidbody2D rb;
Animator anim;
public float maxSpeed = 10f;
bool isGround;
bool isFacingRight = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
anim.SetBool("jump", true);
}
void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
if (move > 0 && !isFacingRight || move < 0 && isFacingRight)
Flip();
anim.SetBool("jump", isGround);
}
void OnTriggerStay2D(Collider2D col)
{
if (col.tag == "ground")
{
isGround = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "ground")
{
isGround = false;
}
}
void Flip()
{
isFacingRight = !isFacingRight;
transform.Rotate(0, 180, 0);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question