How to Implement a Delay for Animation Scripts

Yes I know probably asked 100 times but non of the solutions I’ve found worked for me. Right now I’ve got my character to jump and to land properly, but the thing is every so often it goes right back to the grounded animations just because I hit the space bar not long enough and the script read that the character was grounded while he was moving upwards. My idea is just to delay the scripts reaction for a second but the WaitForSeconds(x) fix didn’t worked whatever way I tried. I dunno what it is with me and delays but they don’t like me, but clearly it mußt be a sure answer it is not like in Java where you need a big threadsleep thing you’ve gotta do with first building the new thread.

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

public class EzyScript : MonoBehaviour
{
    Animator animator;
    int jumpHash = Animator.StringToHash("Jump");
    int doubleJumpHash = Animator.StringToHash("DoubleJump");
    int groundedHash = Animator.StringToHash("GroundedTotally");
    private bool Jumped = false;        //checks if character jumps for doublejump animation
    public float distToGrounded = 15.0f;
    public LayerMask ground;
    Transform tr;


    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        tr = transform;
    }

    //checks if character is grounded
    bool IsGrounded()
    {
        return Physics.Raycast(transform.position, -Vector3.up, distToGrounded);
    }

    void LateUpdate()
    {
        if ((IsGrounded()) && Jumped)
        {
            animator.SetTrigger(groundedHash);
            Jumped = false;
        }
    }

// Update is called once per frame
    void Update()
    {
        float move = Input.GetAxis("Vertical");
        animator.SetFloat("Speed", move);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (Jumped == false)        //if character doesn't jump, it jumps
            {
                animator.SetTrigger(jumpHash);
                animator.SetBool("Grounded", false);
                Jumped = true;
            }
            else                        //if character jumped, performed the doublejump animation
            { 
                animator.SetTrigger(doubleJumpHash);
                //Jumped = false;
            }
        }
    }
}

I guess the animation is the one that’s responsible for controlling the character’s height (i.e. its “physics”).

Normally the IsGrounded() check should only do the raycast while the character is falling. This should allow the character to “break free” from the ground and avoid the problem with immediately going back to the grounded state.

Couple of options:

  1. Keep track of the character’s Y value in IsGrounded(). As soon as the Y value starts decreasing, you know the character’s now falling.

  2. Split up the animation into “jumping_up” and “jumping_falling”, and only let IsGrounded() work while you’re in the falling animation state. Honestly 1 would be the simpler solution if it works for you.

  3. Put the timer into IsGrounded(). I like this option the least because it assumes too much about what’s in the scene around the character (i.e. is there a ceiling which blocks the jump? If so, you risk falling through the world)