Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

coroutine waiting for 2D jumping

Discussion in 'Scripting' started by fosmark13, Aug 29, 2015.

  1. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    hi thanks for the help, i watched the live training 2d about movement and jumping in unity and do the jumping stuff already but when the player jumps a few times suddenly he jumps to high, i read that the way to prevent this situation is to do the jump and then make the character wait a few seconds until he can do a jump again, so i tried with StartCoroutine this is the code:


    Code (CSharp):
    1. ////Jump Button
    2.        
    3.         if (grounded && Input.GetButton ("Jump")) {
    4.            
    5.             StartCoroutine("Jumping");
    6.            
    7.         }
    8.  
    9. IEnumerator Jumping(){
    10.        
    11.  
    12.  
    13.         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
    14.        
    15.         anim.SetBool ("Ground", false);
    16.  
    17.         yield return new WaitForSeconds (2);
    18.  
    19.  
    20.        
    21.        
    22.     }
    I have the If statement in the FixedUpdate area. It does the jump but is not stopping any seconds it keeps continually.

    Thank you
     
  2. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    I would make a bool for checking if you can jump.

    For example

    Code (CSharp):
    1. bool canjump = true;
    then you would just add these two lines in that I've commented placement.

    Code (CSharp):
    1. canjump = false //Line 14
    2.  
    3. yield return new WaitForSeconds(2);
    4.  
    5. canjump = true //On Line 18
    Finally, You would check the canjump bool in your jump if statement, This is the best I can make out, as I'm not sure of any other code you have, but this is a quick fix regardless =)
     
    fosmark13 likes this.
  3. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
     
  4. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91

    Hi again, i tried what you said later and it worked, thanks a lot for the comments this is the code for he If statement:

    Code (CSharp):
    1. if (canJump) {
    2.                
    3.             if (grounded && Input.GetButton ("Jump")){
    4.  
    5.                 StartCoroutine ("Jumping");
    6.  
    7.            
    8.         }
    and the Coroutine:

    Code (CSharp):
    1. IEnumerator Jumping(){
    2.  
    3.         canJump = true;
    4.  
    5.         GetComponent<Rigidbody2D> ().velocity = (new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0f));
    6.  
    7.         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForce));
    8.  
    9.         canJump = false;
    10.  
    11.         anim.SetBool ("Ground", false);
    12.  
    13.         yield return new WaitForSeconds(0.6f);
    14.  
    15.         canJump = true;
    16.        
    17.  
    18.  
    19.  
    20.     }
    thank you
     
    Lorinthio likes this.