Double jump animation not working

Hello,

I am trying to get my player to do a roll animation (that I named doubleJump btw) after he did the first jump and is double jumping but I cant do it.

The most I’ve managed to do is make the animation play on top of the normal jumping animation so the frames of both animation play at the same time.

This is my script, any help would be appreciated :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class player : MonoBehaviour
{
    // Config
    [SerializeField] float climbSpeed = 10f;
    [SerializeField] Vector2 deathKick = new Vector2(25f, 25f);
    [SerializeField] private TrailRenderer tr;
    [SerializeField] private float jumpBufferTime = 0.1f;
    // State
    bool isAlive = true;
    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    public float jumpForce;
    private bool isJumping;
    private float jumpBufferCounter;
    private bool canDoubleJump;

    private float coyoteTime = 0.15f;
    private float coyoteTimeCounter;

    private float moveInput;
    public float speed;

    public GameOver logic;

    private bool canDash = true;
    private bool isDashing;
    private float dashPower = 17f;
    private float dashTime = 0.2f;
    private float dashCooldown = 0.45f;

    private bool isFacingRight = true;



    // Cached component references
    BoxCollider2D myFeet;
    Rigidbody2D myRigidBody;
    CapsuleCollider2D myBodyCollider;
    Animator myAnimator;
    float gravityScaleAtStart;


    // Start is called before the first frame update
    void Start()

    {
        myRigidBody = GetComponent<Rigidbody2D>();
        myBodyCollider = GetComponent<CapsuleCollider2D>();
        myFeet = GetComponent<BoxCollider2D>();
        myAnimator = GetComponent<Animator>();
        gravityScaleAtStart = myRigidBody.gravityScale;
        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<GameOver>();
    }

    void FixedUpdate()
    {
         if (isDashing)
         {
               return;
         }


         moveInput = Input.GetAxis("Horizontal"); //Moving left and right script, set speed in unity editor//
         myRigidBody.velocity = new Vector2 (moveInput * speed, myRigidBody.velocity.y);

        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Run_1", playerHasHorizontalSpeed);
    }

    // Update is called once per frame

    void Update()
    {
        if (isDashing)
        {
           return;
        }

        if (!isAlive) { return; }



        FlipSprite();
        ClimbLadder();
        Die();
        Jump();
        JumpBuffer();
        CoyoteTimer();

        isGrounded = Physics2D.OverlapCircle (feetPos.position, checkRadius, whatIsGround);

        if (Input.GetKeyDown(KeyCode.X) && canDash)
        {
            StartCoroutine(Dash());
        }

        myAnimator.SetFloat("yVelocity", myRigidBody.velocity.y);

    }

    private void Jump()
    {
        if (Input.GetButtonUp("Jump") && myRigidBody.velocity.y > 0f)
        {
            coyoteTimeCounter = 0f;
        }

            if (isGrounded)
        {
            canDoubleJump = true;
        }

        if (Input.GetKey(KeyCode.Space))
        {
            if (isGrounded)
            {
                myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);

                coyoteTimeCounter = 0f;

            }
            else if (Input.GetKeyDown(KeyCode.Space))
            {
                if (canDoubleJump)
                {
                    myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce * 1.3f);
    
                    coyoteTimeCounter = 0f;

                    canDoubleJump = false;

                    myAnimator.SetBool("Jump", false);

                    myAnimator.SetBool("DoubleJumping", true);
                }
                else if (!canDoubleJump)
                {
                     myAnimator.SetBool("DoubleJumping", false);
                     
                    myAnimator.SetBool("Jump", true);
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
           myAnimator.SetBool("Jump", true);
        }

        if (!isGrounded)
        {
            myAnimator.SetBool("Jump", !isGrounded);
            myAnimator.SetBool("Run_1", false);
        }
        else
        {
            myAnimator.SetBool("Jump", false);
            myAnimator.SetBool("DoubleJumping", false);
        }
    }

    private void CoyoteTimer()
    {
        if (isGrounded)
        {
            coyoteTimeCounter = coyoteTime;
        }
        else
        {
            coyoteTimeCounter -= Time.deltaTime;
        }

        if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f && !isJumping)
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);

        }
    }

    private void JumpBuffer()
    {
        if (Input.GetButtonDown("Jump"))
        {
            jumpBufferCounter = jumpBufferTime;
        }
        else
        {
            jumpBufferCounter -= Time.deltaTime;
        }
    }

    private void ClimbLadder()
    {
        if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Climbing")))
        {
            myAnimator.SetBool("Climbing", false);
            myRigidBody.gravityScale = gravityScaleAtStart;
            return;
        }

        float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
        Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
        myRigidBody.velocity = climbVelocity;
        myRigidBody.gravityScale = 0f;

        bool playerHasVerticalSpeed = Mathf.Abs(myRigidBody.velocity.y) > Mathf.Epsilon;
        myAnimator.SetBool("Climbing", playerHasVerticalSpeed);
    }



    private void Die()
    {
        if (myFeet.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))

        {
            isAlive = false;
            myAnimator.SetTrigger("Dying");
            GetComponent<Rigidbody2D>().velocity = deathKick;
            GetComponent<BoxCollider2D>().enabled = false;
            GetComponent<CapsuleCollider2D>().enabled = false;
            logic.gameOver();
            myRigidBody.gravityScale = 1f;
            speed = 0f;
        }
    }


    private void FlipSprite()
    {
        if (isFacingRight && moveInput < 0f || !isFacingRight && moveInput > 0f)
        {
            Vector3 localScale = transform.localScale;
            isFacingRight = !isFacingRight;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }
    }

    private IEnumerator Dash()
    {
        canDash = false;
        isDashing = true;
        float originalGravity = myRigidBody.gravityScale;
        myRigidBody.gravityScale = 0f;
        myRigidBody.velocity = new Vector2(transform.localScale.x * dashPower, 0f);
        tr.emitting = true;
        yield return new WaitForSeconds(dashTime);
        tr.emitting = false;
        myRigidBody.gravityScale = gravityScaleAtStart;
        isDashing = false;
        yield return new WaitForSeconds(dashCooldown);
        canDash = true;
    }
}

I solved it doing this:

private void Jump()
{
    if (Input.GetButtonUp("Jump") && myRigidBody.velocity.y > 0f)
    {
        coyoteTimeCounter = 0f;
    }

    if (Input.GetKey(KeyCode.Space))
    {
        if (isGrounded)
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);

            coyoteTimeCounter = 0f;

            canDoubleJump = true;
        }
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            if (canDoubleJump)
            {
                myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce * 1.3f);
  
                coyoteTimeCounter = 0f;        

                canDoubleJump = false;
            }
        }
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (!canDoubleJump)
        {
            myAnimator.SetBool("DoubleJumping", true);
            myAnimator.SetBool("Jump", false);
        }
    }

    if (isGrounded)
    {
        myAnimator.SetBool("DoubleJumping", false);
        myAnimator.SetBool("Jump", false);
    }
    else
    {
         myAnimator.SetBool("Run_1", false);
    }
}

private void CoyoteTimer()
{
    if (isGrounded)
    {
        coyoteTimeCounter = coyoteTime;
    }
    else
    {
        coyoteTimeCounter -= Time.deltaTime;
    }

    if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f && !isJumping)
    {
        myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);

        canDoubleJump = true;

        myAnimator.SetBool("Jump", true);

        myAnimator.SetBool("DoubleJumping", false);
    }
}

private void JumpBuffer()
{
    if (Input.GetButtonDown("Jump"))
    {
        jumpBufferCounter = jumpBufferTime;
    }
    else
    {
        jumpBufferCounter -= Time.deltaTime;
    }
}