Question | Setting Animator Bool with this ground Check

Hey guys!

I’ve set up a Blend tree for a jump and fall animation in my animator with works well so far.
The problem I have is that in my script I set an “IsJumping”-boolean to true when he leaves the ground and now I am struggling to find a place in my code where I can set the Boolean to “false”.
Here’s what it looks like:

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float playerSpeed = 5f;
    [SerializeField] float jumpSpeed = 5f;
    [SerializeField] float climbSpeed = 5f;
    
    private Vector2 moveInput;
    private Rigidbody2D myRigidbody;
    private Animator myAnimator;
    private CapsuleCollider2D myBodyCollider;
    private BoxCollider2D myFeetCollider;


    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        myBodyCollider = GetComponent<CapsuleCollider2D>();
        myFeetCollider = GetComponent<BoxCollider2D>();
    }


    // Update is called once per frame
    void Update()
    {
        Run();
        FlipSprite();
        ClimbLadder();
        SetYVelocity();

        //myAnimator.SetBool("isJumping", false);    
    }


    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
    }

    
    void OnJump(InputValue value)
    {
        if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        { 
            return;
        }

        if(value.isPressed)
        {
            myAnimator.SetBool("isJumping", true);
            myRigidbody.velocity += new Vector2 (0f, jumpSpeed);
        }
    }


    void Run()
    {
        Vector2 playerVelocity = new Vector2 (moveInput.x * playerSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;

        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

        if (playerHasHorizontalSpeed)
        {
            myAnimator.SetBool("isRunning", true);
        }   

        else
        {
            myAnimator.SetBool("isRunning", false);
        }
        
    }

    void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x), 1f);
        }      
    }

    void ClimbLadder()
    {
        if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }

        Vector2 climbVelocity = new Vector2 (myRigidbody.velocity.x, moveInput.y * climbSpeed);
        myRigidbody.velocity = climbVelocity;
    }

    void SetYVelocity()
    {
        myAnimator.SetFloat("yVelocity", myRigidbody.velocity.y);
    }
}

If someone has some advice for me that would be ace!
Thanks!

Hi,

Maybe it’ll help to define “IsJumping” a bit more precisely in simple English. As a human, I know what you mean, so your explanation will not be for me. You need to explain your idea to the computer. The computer does not even know what “jumping” means, so you’ll have to explain every single step.

Next, you have to define what “not jumping” means. It’s basically the “opposite” of your definition of “jumping”. The last step is to check your code to figure out where the player is “not jumping”. And that will be the right place to set the boolean to false.


See also:

Hey Nina!

Thanks for your reply.
“IsJumping” is the boolean parameter in the Unity Animator:
Bildschirmfoto 2023-05-30 um 12.10.33

I have already assessed that the best place to set the Jumpanimation to “false” would be the ground check.

And I applied the boolean command right there in it before the “return” command. Like so:

 if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        { 
            myAnimator.SetBool("isJumping", false); 
            return;
        }

But it does absolutely zero.

And that’s where my struggle sets in (and probably my lack of understanding shows. :D).

Ah, ok. Yes, the code makes sense so far. However, since it does not give you the expected result, I would suggest to add a Debug.Log before the return; to see whether the if-block gets executed.

And if it gets executed, I would set another bool to the code where “isJumping” gets set to true. Maybe your code does work. The code gets executed dozens of times per second. If “isJumping” gets set to false for a fraction of a second, you simply don’t see that your code works.

If the problem is that “isJumping” gets set to true unexpectedly, you’ll have to analyse the code further.

When writing code, always bear in mind that your ideas could make perfect sense, and your code could make perfect sense, but the result is not what you expected because a lot of other things get executed as well. Debug.Log is your friend to figure out what is going on. :slight_smile:

1 Like

Thanks Nina.
I’ll give it a shot and get back to you! :slight_smile:

Privacy & Terms