How to set bool value to false after one frame

My goal is prevent holding jump key.

Using Coroutine :

    private InputSystem inputSystem;
   
    [HideInInspector] public bool jump;
      
    private void OnEnable()
    {
        if (inputSystem == null)
        {
            inputSystem = new InputSystem();

            inputSystem.Player.Jump.performed += i => StartCoroutine(Jump());
            inputSystem.Player.Jump.canceled += i => CancelJump();
        }

        inputSystem.Enable();
    }
   
    private IEnumerator Jump()
    {
        jump = true;

        yield return new WaitForEndOfFrame();

        jump = false;
    }
    private void CancelJump()
    {
        jump = false;
    }

Using async :

    private InputSystem inputSystem;
   
    [HideInInspector] public bool jump;
      
    private void OnEnable()
    {
        if (inputSystem == null)
        {
            inputSystem = new InputSystem();

            inputSystem.Player.Jump.performed += i => Jump();
        }

        inputSystem.Enable();
    }
   
    private async void Jump()
    {
        jump = true;

        while (jump)
        {
            await Task.Yield();

            jump = false;
        }    
    }

I am very new to coding and unity. Please explain. Thank you.

  1. Is it okey to use coroutine or async for this case?
  2. If it is okey to use, which is better for this case?
  3. Are there any other alternatives?

The way to wait one frame in a coroutine is yield return null;

I don’t really understand why you need all this complexity though. Just perform your jump in the performed callback, or set a bool to true that gets consumed by the actual jumping code (set it to false when the jump is performed).

1 Like

Yeah, I’m really confused by all of this too.

Why not just use getbuttondown in update?

Coroutines are usually for repetitive actions that you don’t want eating up your cpu, like ai or loading things.

At least in my experience.

You seem fairly knowledgeable so forgive me if I sound condescending, which is not my intention, but:

getbuttondown performs an action on the frame where the button is pressed.

getbutton performs every frame the button is held.

getbuttonup performs when the button is lifted.

1 Like

Whenever i press jump key player can’t jump. I don’t know why. Only jumps when i press multiple times.

inputSystem.Player.Jump.performed += i => Jump();

    private void Jump()
    {
        playerVelocity = new Vector3(playerVelocity.x, 0f, playerVelocity.z);

        if (collision.CheckTop())
        {
            playerVelocity += Vector3.up * 0.0f;
        }
        else if (!collision.CheckTop())
        {
            playerVelocity += Vector3.up * movementSettings.jumpingForce;
        }
    }

Thats why i am using bool:

private void Movement()
    {   
        if (CheckGround())
        {
            if (input.jump)
            {
                JumpMovement();
            }
        }
        else
        {
            FallMovement();
        }
characterController.Move(playerVelocity * Time.deltaTime);
    }   
    private void JumpMovement()
    {
        playerVelocity = new Vector3(playerVelocity.x, 0f, playerVelocity.z);

        if (CheckTop())
        {
            playerVelocity += Vector3.up * 0.0f;
        }
        else if (!CheckTop())
        {
            playerVelocity += Vector3.up * movementSettings.jumpingForce;
        }
    }

Thank you for answering.