- Home /
 
 
               Question by 
               AlexanderChereji · Nov 19, 2021 at 06:41 PM · 
                movementgameobjects  
              
 
              How do I make a realistic jump?
I am a beginner, I don't really know how to code in c#. Can somebody help me making a realistic jump for my game? I tried everything but it just won't work. I already have a moving script but really want to add a jump:
 public class Movement : MonoBehaviour
 {
     float speed = 10;
     public GameObject player;
     void Update ()
     {
         // making vars of getting axis value
         float horizontal_movement = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         float vertical_movement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         float jump_movement = Input.GetAxis("Jump");
         
         // moving parts at X and Z Axis
         transform.Translate(horizontal_movement, 0, vertical_movement);
         player.transform.Translate(horizontal_movement, 0, vertical_movement);
         
         // moving parts at Y Axis
         //transform.Translate(0, jump_movement, 0);
         //player.transform.Translate(0, jump_movement, 0);
     }
 }
 
              
               Comment
              
 
               
              Answer by benjmitch · Nov 19, 2021 at 10:43 PM
It really depends on the context, but a cheap and cheerful way is to maintain a height above some surface, and the rate of travel upward, then accelerate downwards constantly, limit height to be >= 0, and inject an impulse into the rate when the player presses jump:
 const float jump_speed = 5;
 float height=0;
 float vspeed=0;
 void FixedUpdate()
 {
     float T = Time.fixedDeltaTime;
     const float gravity = -10;
     vspeed += T * gravity;
     if (player_pressed_jump)
         vspeed += jump_speed;
     height += T * vspeed;
     height = Mathf.Max(height, 0);
     //  lay this in where required, e.g.
     transform.position = new Vector3(0, height, 0);
 }
 
              Your answer