Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Jumping using rigidbody.velocity question.

Discussion in 'Scripting' started by hizral, Mar 16, 2012.

Thread Status:
Not open for further replies.
  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there.

    have a few question here, I'm trying to do a jumping script for my character using a rigidbody. for the jumping part I manage to do it, below is the script :
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. class CharacterJump
    5. {
    6.     public var jumping : boolean;
    7.     public var minHeight : float;
    8.     public var maxHeight : float;
    9.     public var gravity : float;
    10. }
    11. public var jump : CharacterJump;
    12.  
    13. function Start ()
    14. {
    15.     jump.jumping = true;
    16.    
    17.     //This will apply the gravity for character
    18.     Physics.gravity = Vector3.up * jump.gravity;
    19. }
    20.  
    21. function Update ()
    22. {
    23.     ApplyJump ();
    24.     ApplyExtraJump ();
    25. }
    26.  
    27. function ApplyJump ()
    28. {
    29.     if (jump.jumping)
    30.     {
    31.         if (Input.GetButtonDown ("jump"))
    32.         {
    33.             rigidbody.velocity = Vector3.up * jump.minHeight;
    34.         }
    35.     }
    36.    
    37.     if (rigidbody.velocity.magnitude > 0.1)
    38.     {
    39.         jump.jumping = false;
    40.     }
    41.     else
    42.     {
    43.         jump.jumping = true;
    44.     }
    45. }
    46.  
    47. function ApplyExtraJump ()
    48. {
    49.    
    50. }
    51.  
    As for now my character will jump at min height if I push the jump button, now what I want to do next is when I hold down the jump button a bit longer it will add the jump height to max height. How would I do this? I tried used timer for the button hold but it did not work.
     
  2. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    Line 17 seems a bad idea - Physics.gravity is global, you don't want individual object scripts to try to modify it directly.

    To make the character stay in the air longer, apply an upwards force (less than gravity times mass!) while the button is held down. You could do this by modifying velocity again, but I'd steer clear of that in general.
     
  3. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    So instead of using Physics.gravity. I change it to this :
    Code (csharp):
    1.  
    2. function ApplyJump ()
    3. {
    4.     if (jump.jumping)
    5.     {
    6.         if (Input.GetButtonDown ("jump"))
    7.         {
    8.             rigidbody.velocity = Vector3.up * jump.minHeight;
    9.         }
    10.     }
    11.    
    12.     if (rigidbody.velocity.magnitude > 0.1)
    13.     {
    14.         jump.jumping = false;
    15.        
    16.         rigidbody.velocity.y -= jump.gravity * Time.deltaTime;
    17.     }
    18.     else
    19.     {
    20.         jump.jumping = true;
    21.     }
    22. }
    23.  

    Im not fully understand this. Can you please elaborate. Thank you.
     
  4. tigerfoot

    tigerfoot

    Joined:
    Nov 2, 2009
    Posts:
    132
    Here is the code. When you press space it jumps and if you hold space for 1 second (play with that value) it jumps again.
    Dont change gravity directly nor change rigidbody.velocity for this matter. Use AddForce or AddRelativeForce for jump and movement.

    Code (csharp):
    1.  
    2.     #pragma strict
    3.     private var jumpCounter : float;
    4.    
    5.     class CharacterJump
    6.     {
    7.         //public var jumping : boolean;
    8.         public var jumpForce : float;
    9.         public var extraJumpForce : float;
    10.         //public var gravity : float;
    11.         public var jumped : boolean; // if you jumped
    12.         public var jumpedMore : boolean; // if you jumped more
    13.     }
    14.     public var jump : CharacterJump;
    15.      
    16.     function Start ()
    17.     {
    18.     }
    19.      
    20.     function Update ()
    21.     {
    22.         if(Input.GetButton ("Jump")){
    23.             if(!jump.jumped){
    24.                 jump.jumped = ApplyJump();
    25.             }
    26.             else{
    27.                 // if you hold space for   1 second   make extra jump
    28.                 if(jump.jumpedMore == false  jumpCounter > 1){
    29.                      jump.jumpedMore = ApplyExtraJump ();
    30.                 }
    31.                 else{
    32.                     jumpCounter += 1*Time.deltaTime;
    33.                 }
    34.             }
    35.         }
    36.         if(Input.GetButtonUp ("Jump")){
    37.             // get ready for next jump when you release jump key
    38.             jump.jumped = false;
    39.             jump.jumpedMore = false;
    40.             jumpCounter = 0;
    41.         }
    42.     }
    43.      
    44.     function ApplyJump () : boolean
    45.     {
    46.         rigidbody.AddForce(Vector3.up * jump.jumpForce); // add upward force instead of changing gravity
    47.         return true; // normal jump executed
    48.     }
    49.      
    50.     function ApplyExtraJump () : boolean
    51.     {
    52.         rigidbody.AddForce(Vector3.up * jump.extraJumpForce);
    53.         return true; // extra jump executed
    54.     }
    55.  
     
    Last edited: Mar 16, 2012
  5. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hey there guys, okay I manage to do jump for my character, here what I did :
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. private var jumpCounter : float;
    5. public var minHeight : float;
    6. public var maxHeight : float;
    7. public var gravity : float;
    8. public var canJump : boolean;
    9. public var jump : boolean;
    10.  
    11. function Start ()
    12. {
    13.     canJump = true;
    14. }
    15.  
    16. function Update ()
    17. {
    18.     ApplyJump ();
    19.     ApplyGravity ();
    20. }
    21.  
    22. function ApplyJump ()
    23. {
    24.     if (canJump)
    25.     {
    26.         if (Input.GetButton ("jump"))
    27.         {
    28.             rigidbody.velocity = Vector3.up * minHeight;
    29.  
    30.             jumpCounter += 1 * Time.deltaTime;
    31.         }  
    32.     }
    33.    
    34.     if (canJump  jumpCounter > 0.1)
    35.     {
    36.         rigidbody.velocity.y += maxHeight;
    37.         canJump = false;
    38.     }
    39.    
    40.     if (Input.GetButtonUp ("jump"))
    41.     {
    42.         jumpCounter = 0;
    43.         canJump = true;
    44.     }
    45. }
    46.  
    47. function ApplyGravity ()
    48. {
    49.     if (rigidbody.velocity.magnitude > 0.1)
    50.     {
    51.         rigidbody.velocity.y -= gravity * Time.deltaTime;
    52.     }
    53.    
    54.     if (Physics.Raycast (transform.position, -transform.up, 1))
    55.     {
    56.         jump = true;
    57.     }
    58.     else
    59.     {
    60.         jump = false;
    61.     }
    62. }
    63.  
    Having a bit of trouble figureing out this one thing, how do I prevent people from spamming the jump button, because right now, if I keep on tapping the jump button my chracter will keep on jumping up and up and up until i release the jump button. Down here I show you guys what I meant.


    Thanks
     
  6. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
  7. Linetsis

    Linetsis

    Joined:
    Oct 25, 2013
    Posts:
    4
    Bit Late, but you can put a trigger on the ground which sends a message to the object so this one knows whether it can jump or not (you can use a boolean to check this).
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    bit late. crappy solution. what was the point.

    only necro if you have something of quality to add.
     
Thread Status:
Not open for further replies.