Jump animation only plays for about 1/10 of a second unless the button is pressed twice

I just started learning how to use Unity. I’m working on a 2D platformer using tutorial videos from the youtube channel “Brackeys”. My method is to go through one video, following his instructions to a T, and repeating that same video through multiple iterations before moving on to the next video. For a reason that i cant figure out, one of my iterations wont properly play the jumping animation.

In the animation preview window it looks perfectly fine, but when doing a test run the animation will only play for a split second before reverting back to the animation before it (either run or idle). Although it will play all the way through if I press the button twice. When looking at the parameters tab in the animation window, I can see the “IsJumping” parameter is activated for that split second when i press the jump button but then it unchecks immediately after. The weirdest part is that I’ve checked about five times and as far as I can see everything is exactly the same between the iteration that doesnt work and the one that does. Any help or advice would be greatly appreciated!

Here is the movement script:

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

public class Playermovement : MonoBehaviour {

    public CharacterController2D controller;
    public Animator animator;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

    // Update is called once per frame
    void Update() {

        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
        
        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
            animator.SetBool("IsJumping", true);
        }

        if (Input.GetButtonDown("Crouch"))
    {
           crouch = true;
    } else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
    }

     }

    public void OnLanding()
    {
        animator.SetBool("IsJumping", false);
    }

   public void OnCrouching (bool isCrouching)
   {
       animator.SetBool("IsCrouching", isCrouching);
   }
    void FixedUpdate ()
   {
       // Move our character
       controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
       jump = false;
      }
}

@Trevor_trev I figured it out! My issue was not in the PlayerMovement, it is in the CharacterController.
In the FixedUpdate, there is this part:

            m_Grounded = true;
            if (!wasGrounded)
                OnLandEvent.Invoke();
        }

get rid of that “!” I am not sure why his code had it but it should not be there.

            m_Grounded = true;
            if (wasGrounded)
                OnLandEvent.Invoke();
        }

Hope this was the same issue for you and this fixes it!

And here i am again - with an answer - in the CharacterController2D Script, that all of you, who rebuilt the Brackeys stuff, should have - there is a section called:

// If the player should jump...
		if (m_Grounded && jump)
		{
			// Add a vertical force to the player.
			m_Grounded = false;
			m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
		}

Please correct the false to true…
Reason:
The script checks constantly if the Player isGrounded - which you have to set by giving the component a material or a collider and set this to “WhatIsGround” - you should have this in the Inspector window under your CharacterController2D script as a float.

Now, the script asks the following:

If the Player is grounded and enters jump state please AddForce to the Rigidbody called JumpForce. (also to be set in the inspector.

Now the script also says, which is funny - m_Grounded=false;
which doesnt make any sense at all. So in my case it only showed the jump anymation when im in midair, so to say NOT GROUNDED - which was correct in terms of the script.

So please set it to

m_Grounded = true;

and the problem should be corrected.

This is my state of knowledge and i think there are many others out there knowing better - but hey - i wrote this! you not! ^^

Have fun!