Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make Character jump height independent of gravity?

Discussion in 'Scripting' started by MosquitoByte, Mar 25, 2019.

  1. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    Just making a simple platform game, with a script to handle movement and jumping by the player. The player has a collider on the bottom as a trigger to detect when it collides with anything tagged Ground. Everything runs alright. except the jumps seem floaty. I want to make the player fall down from a jump faster and sooner than they currently do, but increasing the gravity makes the jump height shorter, which i dont want

    the code is as follows:

    Code (CSharp):
    1. public class garbageEngine : MonoBehaviour {
    2.  
    3.     Rigidbody2D rb;
    4.     float speed;
    5.     public bool isgrounded = false;
    6.  
    7.     float verticalvelocity;
    8.     float gravity = 14f;
    9.     float jumpforce = 20f;
    10.  
    11.     void Start()
    12.     {
    13.         rb = gameObject.GetComponent<Rigidbody2D>();
    14.         speed = 10f;
    15.     }
    16.  
    17.     void OnTriggerEnter2D(Collider2D collision)
    18.     {
    19.         if (collision.gameObject.tag == "Ground")
    20.         {
    21.             isgrounded = true;
    22.            
    23.         }
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         Debug.Log("vertical velocity is " + verticalvelocity);
    29.  
    30.         rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * speed, rb.velocity.y);
    31.  
    32.         if(isgrounded == true)
    33.         {
    34.             verticalvelocity = -gravity * Time.deltaTime;
    35.  
    36.             if (Input.GetKeyDown(KeyCode.Space))
    37.             {
    38.                 verticalvelocity = jumpforce;
    39.                 isgrounded = false;
    40.             }
    41.         }
    42.         else
    43.         {
    44.             verticalvelocity -= gravity * Time.deltaTime;
    45.         }
    46.  
    47.         Vector2 movejump = new Vector2(rb.velocity.x, verticalvelocity);
    48.         rb.velocity = movejump;
    49.  
    50.     }
    51.  
    52. }
     
  2. trisogene

    trisogene

    Joined:
    Mar 18, 2019
    Posts:
    2
    u should check if your rb.velocity.y it's negative , if it is then gravity is high , if it's not then gravity is low c:
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think its the square root of (-2 * wantedJumpHeight * g), but i may be off by a bit, ill double chexk my code when im home if this is incorrect(or someone can correct me).

    You could also make the player kinematic and lerp its height if it fits your controller.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I just realized that the mass is not in the equation, i think the case is multiplying the value given by the square root by the mass.
     
  5. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    this is very confusing.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    It's possible to invent jumping rules that don't depend on gravity, but that implies that there are at least some times during the jump that the jumper is not experiencing normal gravitational acceleration.

    A more typical way of making a jump less "floaty" would be to increase gravity and also increase the initial force of the jump to compensate.

    In principle, you could calculate the appropriate jumping force to reach a given height based on the amount of gravity, but it's probably easier just to try different amounts of jumping force experimentally until you find one that looks good to you.
     
  7. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    I'll give that a shot, thanks for the suggestion. i just prefer working with small numbers, so i was looking if there was a different solution before i tried that
     
  8. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    Alright, so ive been trying what @Antistone suggested and it works quite well, but there's one last problem. Too much gravity seems to slow down horizontal movement. Is there some sort of crossover between the X and Y velocities that would cause this? can i make x and y work independently of each other?
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    To make my previous post more concise, if you want to specify a height and add an upwards force to get to said height with a character of particular weight in a particular g environment you can do it like so:

    this function should give you the needed force:
    Code (CSharp):
    1. public float JumpForceThingyCalculator(float wantedHeight, float weight, float g){
    2. return weight * Mathf.Sqrt( -2 * wantedHeight * g);
    3. }
    to make a character jump to 10m something like this:
    Code (CSharp):
    1. public float wantedJumpHeight;
    2. bool jump = false;
    3. Rigidbody rb;
    4. void Start(){
    5. rb = GetComponent<Rigidbody>();
    6. }
    7. void Update(){
    8. if(Input.GetKeyDown(KeyCode.Space))
    9. jump = true;
    10. }
    11. void FixedUpdate(){
    12.  
    13. if(jump){
    14. rb.AddForce(Vector3.Up * JumpForceThingyCalculator( wantedJumpHeight, rb.mass, -Physics.Gravity.y), ForceMode.Impulse);
    15. jump = false;
    16. }
    17. }
    *untested
     
    swingingtom likes this.
  10. swingingtom

    swingingtom

    Joined:
    Feb 15, 2018
    Posts:
    9
    @SparrowGS thanks for the snippet.

    It doesn't work AS IS for me as it produces a NaN from sqrt of negative number.
    Simply remove the negative sign of the
    -Physics.Gravity.y
    did the trick.

    Cheers !