- Home /
 
Jump Unpredictable
Hello, I have a script, that controls both the gravity and the jumping. I am not using anything like character controller or anything, and it is a simple
 function Jump () {
     if(down){
                     rigidbody.AddForce(transform.up * 800 * 10 * 8 * Time.deltaTime);
     }else{
                 rigidbody.AddForce(transform.up * -800 * 10 * 8 * Time.deltaTime);
     }
 }
 
 function Gravity () {
     if(down){
         rigidbody.AddRelativeForce(transform.up * -10 * 10 * 8 * Time.deltaTime);
     }else{
         rigidbody.AddRelativeForce(transform.up * 10 * 10 * 8 * Time.deltaTime);
     }
 }
 
               The problem is that the jump will sometimes be super short or really high. I normally would have simply used rigidbody for gravity but the game focuses on flipping gravity.
Answer by Amon · Apr 17, 2014 at 09:55 PM
 #pragma strict
 
 @HideInInspector
 var Gravity : float;
 @HideInInspector
 var JumpHeight : float;
 @HideInInspector
 var CanJump : int;
 @HideInInspector
 var falling : int;
 @HideInInspector
 var IsJumping : int;
 @HideInInspector
 var DoJump : int;
 
 
 function Start () {
     
     CanJump = 1;
     Gravity = 3.0;
     JumpHeight = 1.0;
     falling = 0;
     IsJumping = 0;
     DoJump = 0;
     
 }
 
 function Update () {
 
 }
 
 function FixedUpdate () {
     ActivateJump();
 }
 
 function ActivateJump() {
     if ( Input.GetKey(KeyCode.Space) && CanJump == 1 ) {
         if ( IsJumping == 0 ){
             IsJumping = 1;
             CanJump = 0;
             DoJump = 1;
         }
     }
     
     if ( DoJump == 1 ) {
         Jump();
     }
 }
 
 function Jump() {
     transform.position.y += JumpHeight;
     JumpHeight = JumpHeight - Gravity * Time.deltaTime;
     
     if ( JumpHeight < 0 ) {
         falling = 1;
         //do Collision checks when falling;
         //when you hit floor or platform reset everything
         FloorCollisionCheck();        
     }
 }
 
 
 function FloorCollisionCheck() {
     //just s imple function to test if the y location is below 0.
     // If it is then we stop and reset jumping;
     
     if ( transform.position.y <= 0 && falling == 1) {
         falling = 0;
         CanJump = 1;
         JumpHeight = 1.0;
         IsJumping = 0;
         DoJump = 0;
     }
 }
 
               Don't use too high a value for JumpHeight or too low a value for Gravity when multiplying by Time.DeltaTime.
Yes it doesn't use built-in physics stuff in unity but it should put you on the correct track.
Yes! Do the necessary checks in the FloorCollisionsCheck function. Or set up a new Function called, for example: ColliderCheck();.
Let us know if you have any problems. If you need an example which works with colliders I can produce one but not till tomorrow as I'm trying to get a project ready for release.
Your answer
 
             Follow this Question
Related Questions
Why does my characterController code not apply gravity correctly? 0 Answers
Apply gravity to my character through script. 0 Answers
How to check if your character is falling 2D - C# 1 Answer
My jump script doesn't work every time 3 Answers
gravity doesnt seem to effect my first person controller....help! 1 Answer