Search Unity

Resolved How to set bool value to false after one frame

Discussion in 'Scripting' started by Deleted User, Aug 20, 2021.

  1. Deleted User

    Deleted User

    Guest

    My goal is prevent holding jump key.

    Using Coroutine :
    Code (CSharp):
    1.     private InputSystem inputSystem;
    2.    
    3.     [HideInInspector] public bool jump;
    4.      
    5.     private void OnEnable()
    6.     {
    7.         if (inputSystem == null)
    8.         {
    9.             inputSystem = new InputSystem();
    10.  
    11.             inputSystem.Player.Jump.performed += i => StartCoroutine(Jump());
    12.             inputSystem.Player.Jump.canceled += i => CancelJump();
    13.         }
    14.  
    15.         inputSystem.Enable();
    16.     }
    17.    
    18.     private IEnumerator Jump()
    19.     {
    20.         jump = true;
    21.  
    22.         yield return new WaitForEndOfFrame();
    23.  
    24.         jump = false;
    25.     }
    26.     private void CancelJump()
    27.     {
    28.         jump = false;
    29.     }

    Using async :
    Code (CSharp):
    1.     private InputSystem inputSystem;
    2.    
    3.     [HideInInspector] public bool jump;
    4.      
    5.     private void OnEnable()
    6.     {
    7.         if (inputSystem == null)
    8.         {
    9.             inputSystem = new InputSystem();
    10.  
    11.             inputSystem.Player.Jump.performed += i => Jump();
    12.         }
    13.  
    14.         inputSystem.Enable();
    15.     }
    16.    
    17.     private async void Jump()
    18.     {
    19.         jump = true;
    20.  
    21.         while (jump)
    22.         {
    23.             await Task.Yield();
    24.  
    25.             jump = false;
    26.         }    
    27.     }
    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?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    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).
     
    Deleted User likes this.
  3. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    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.
     
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

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

    Code (CSharp):
    1. inputSystem.Player.Jump.performed += i => Jump();
    2.  
    3.     private void Jump()
    4.     {
    5.         playerVelocity = new Vector3(playerVelocity.x, 0f, playerVelocity.z);
    6.  
    7.         if (collision.CheckTop())
    8.         {
    9.             playerVelocity += Vector3.up * 0.0f;
    10.         }
    11.         else if (!collision.CheckTop())
    12.         {
    13.             playerVelocity += Vector3.up * movementSettings.jumpingForce;
    14.         }
    15.     }
    Thats why i am using bool:

    Code (CSharp):
    1. private void Movement()
    2.     {  
    3.         if (CheckGround())
    4.         {
    5.             if (input.jump)
    6.             {
    7.                 JumpMovement();
    8.             }
    9.         }
    10.         else
    11.         {
    12.             FallMovement();
    13.         }
    14. characterController.Move(playerVelocity * Time.deltaTime);
    15.     }  
    16.     private void JumpMovement()
    17.     {
    18.         playerVelocity = new Vector3(playerVelocity.x, 0f, playerVelocity.z);
    19.  
    20.         if (CheckTop())
    21.         {
    22.             playerVelocity += Vector3.up * 0.0f;
    23.         }
    24.         else if (!CheckTop())
    25.         {
    26.             playerVelocity += Vector3.up * movementSettings.jumpingForce;
    27.         }
    28.     }
    Thank you for answering.
     
    Last edited by a moderator: Aug 20, 2021