T
T
Taisu Black2020-07-10 15:45:45
Unity
Taisu Black, 2020-07-10 15:45:45

How to control animation speed?

The problem is that when my character jumps the animation works fine but if my character has a higher jump then the animation plays faster and the character lands with a rest animation. What I need is that no matter how long the character was in the air, the jump animation should be played. Sorry if I'm a little unclear!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;
    Animator anim;

    public float MoveSpeed;
    private float ControllerMove;

    public float JumpForce;

    public Transform GroundCheck;
    public LayerMask whIsGround;
    public bool Grounded = false;
    public float GroundRadius;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>(); 
        anim = GetComponent<Animator>();  
    }

    void Update()
    {   
        Grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, whIsGround);

        ControllerMove = Input.GetAxis("Horizontal");

        if(Input.GetKeyDown(KeyCode.Space) && Grounded)
        {
            Jump();
            anim.SetBool("Jump", true);
        }
         
        else if(Grounded == true)
        {
            anim.SetBool("Jump", false);
        }  
    

    }
    private void FixedUpdate()
    {
        rb.velocity = new Vector2(ControllerMove * MoveSpeed, rb.velocity.y);

        if(Input.GetAxis("Horizontal") == 0)
        {
            anim.SetBool("Run", false);
        }
        else
        {   
            FlipSpite();
            anim.SetBool("Run", true);
        }
    }
    private void Jump()
    {
        rb.velocity += JumpForce * Vector2.up; 
    }

    private void FlipSpite()
    {
        if(Input.GetAxis("Horizontal") > 0)
        {
            transform.localRotation = Quaternion.Euler(0, 0, 0);
        }
        if(Input.GetAxis("Horizontal") < 0)
        {
            transform.localRotation = Quaternion.Euler(0, 180, 0);
        }
    }
}


5f0862d9afb7c103976883.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question