Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question jump to a specific height.

Discussion in 'Physics' started by Alexey045, May 2, 2024.

  1. Alexey045

    Alexey045

    Joined:
    Jun 4, 2022
    Posts:
    8
    Hello everyone! I was interested in learning how to perform a jump to a specific height. To achieve this, I used the Jump method.

    I derived the formula for calculating the jump height based on the specified number of units in the _jumpHeight variable. I used the ballistics formula to find the maximum height, which is h = V^2 / 2 * g. However, in order to make the character jump to the desired height in Unity, I had to subtract (Time.fixedDeltaTime * Physics.gravity.y) / 2. This works, but I'm not sure why.

    Could someone please explain to me why this subtraction is necessary? I would appreciate a detailed explanation. I would also appreciate any other methods for performing a jump to a specified height. Thank you in advance!

    Here's the code for my Jump method:
    Code (CSharp):
    1. public void Jump(InputAction.CallbackContext context)
    2. {
    3.     // I don't know why this crappy DeltaTime * gravity / 2 is used
    4.     var velocity = Mathf.Sqrt(_jumpHeight * -2 * Physics.gravity.y) - (Time.fixedDeltaTime * Physics.gravity.y / 2);
    5.     if (context.started && IsGrounded())
    6.     {  
    7.         _rb.AddForce(0f, velocity, 0f, ForceMode.VelocityChange);
    8.     }
    9. }
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Do you have a drag set on the RigidBody, if any?

    With a jump height of 1 m you have:
    v = 4.43+0.10
    0.10 makes the v only a little over 2% faster.
     
  3. Alexey045

    Alexey045

    Joined:
    Jun 4, 2022
    Posts:
    8
    My RigidBody drag is set to zero. Angular drag doesn't affect on jump height.

    It seems to me that Unity handles physics specific, so I need to adjust the formula. Perhaps @MelvMay could help me figure this out? Here's the code for 2D version:
    Code (CSharp):
    1. using Unity.Mathematics;
    2. using UnityEngine;
    3.  
    4. public class Movement : MonoBehaviour
    5. {
    6.     [SerializeField, Range(0, 20)] float _jumpHeight = 5f;
    7.     Rigidbody2D _rb;
    8.     float _maxHeight = 0f;
    9.  
    10.     void Awake()
    11.     {
    12.         _rb = GetComponent<Rigidbody2D>();
    13.         _maxHeight = _rb.position.y;
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.Space))
    19.         {
    20.             Jump();
    21.         }
    22.  
    23.         _maxHeight = Mathf.Max(_maxHeight, _rb.position.y);
    24.         if (_rb.position.y < _maxHeight)
    25.         {
    26.             print(_maxHeight);
    27.         }
    28.     }
    29.  
    30.     void Jump()
    31.     {
    32.         var impulse = _rb.mass * (math.sqrt(-2 * Physics2D.gravity.y * _jumpHeight) - (Time.fixedDeltaTime * Physics.gravity.y / 2));
    33.  
    34.         if (IsGrounded())
    35.         {
    36.             _rb.AddForce(new Vector2(0f, impulse), ForceMode2D.Impulse);
    37.         }
    38.     }
    39.  
    40.     private bool IsGrounded()
    41.     {
    42.         return _rb.velocity.y == 0;
    43.     }
    44. }
    45.  
    Here are my RigidBody and Rigidbody2D settings:
    upload_2024-5-3_16-53-11.png upload_2024-5-3_16-55-37.png

     
    Last edited: May 3, 2024