Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Inconsistent Jumping Height

Discussion in 'Scripting' started by Arthur_Unity3D, Feb 1, 2018.

  1. Arthur_Unity3D

    Arthur_Unity3D

    Joined:
    Feb 1, 2018
    Posts:
    3
    Hello everyone. I'm having trouble with making my character jump. I can make him jump but the height of that jump is inconsistent. I don't want him to jump very high, but sometimes he just rockets up into the air, and I don't know what is causing it. Here's my code. I took out everything except what related to jumping.
    Code (CSharp):
    1. public int jumpSpeed; //Jump speed.
    2. private bool canJump; //Whether the player can jump or not.
    3. private Rigidbody rb; //Gives access to the rigidbody.
    4. void Start(){ //Specifies the Rigidody.
    5.         rb = GetComponent<Rigidbody>();
    6.     }
    7. void FixedUpdate () {
    8.     PlayerJump();
    9. }
    10. void PlayerJump(){ //Controls jumping.
    11.     if((canJump == true) && (Input.GetKeyDown(KeyCode.Space))){
    12.         Vector3 jump = new Vector3(0, jumpSpeed, 0);
    13.         rb.AddForce(0f, 0f, 0f);
    14.         rb.AddForce(jump, ForceMode.Impulse);
    15.     }
    16. }
    17. //Checks if the player is on ground.
    18. void OnCollisionEnter(Collision other){
    19.     if(other.gameObject.tag == "Ground"){
    20.         canJump = true;
    21.     }
    22. }
    23.  
    24. void OnCollisionExit(Collision other){
    25.     canJump = false;
    26. }
    27. //==================================
    28.  
    Any help would be greatly appreciated.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    A few things that could help might be:

    a) Check for the jump input in Update(). You can safely add a 1 time force in Update(), also, unless there is more in FixedUpdate that was removed for your post. If that's the case, and you'd rather have it there, then you can set a bool value to true, for the jump input, and once it's "consumed" in FixedUpdate() set it back to false. This could prevent possibly having 2 fixed updates run in a row - maybe that was your issue, I'm not sure.

    b) You might want to check that you left the proper tag when setting 'canJump' to false in the exit collision.

    c) It might be better still to check if you're grounded with a small raycast; I find that's a lot easier, but to each their own.
     
  3. Arthur_Unity3D

    Arthur_Unity3D

    Joined:
    Feb 1, 2018
    Posts:
    3
    Thanks for the answer methos5k! I tried what you said and changed it to this:
    Code (CSharp):
    1.     void PlayerJump(){ //Controls jumping.
    2.         if((canJump == true) && (Input.GetKeyDown(KeyCode.Space))){
    3.             Vector3 jump = new Vector3(0, jumpSpeed, 0);
    4.             rb.AddForce(0f, 0f, 0f);
    5.             rb.AddForce(jump, ForceMode.Impulse);
    6.             canJump = false;
    7.         }
    8.     }
    After putting canJump to false after adding force it hasn't had the inconsistent jump at all! I really appreciate the help! As for using a raycast, I saw a lot of places suggesting that but I'm just kinda tinkering right now. Anyway thanks again for the help!
     
    A_Marraff and Siggytron like this.
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :)

    I once had built a character with a similar design to yours and eventually went back to a raycast, but we all have our own journeys :)

    I'm glad that I could help, and that it's working better for you now.

    1 small thing, which is probably not hurting anything, is that I'm 90+% sure that the line "rb.AddForce(0,0,0)" does nothing ;)
     
    Arthur_Unity3D likes this.
  5. Arthur_Unity3D

    Arthur_Unity3D

    Joined:
    Feb 1, 2018
    Posts:
    3
    Oops, your right. That line is pointless. Well, live and learn. Thanks again. :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, not a big deal just thought I'd mention it. :) Take care.
     
    Arthur_Unity3D likes this.
  7. Siggytron

    Siggytron

    Joined:
    Feb 9, 2018
    Posts:
    12
    Thanks for putting up your code fix. This helped me.
     
    Carb0nPhyber likes this.
  8. Siggytron

    Siggytron

    Joined:
    Feb 9, 2018
    Posts:
    12

    Thank you! That was so helpful. I had the exact same problem and the boolean in Update tactic worked for me.
     
  9. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    My Solution
    Right Before you jump you have to cancel the velocity on y axis

    Code (CSharp):
    1. public void Jump()
    2.     {
    3.         if (!isGrounded)
    4.             return;
    5.         rb.velocity = new Vector2(rb.velocity.x, 0f);
    6.         rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    7.     }
    Also you have to cancel velocity when you set isGrounded to true.

    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Platform")
    4.         {
    5.             isGrounded = true;
    6.             rb.velocity = Vector3.zero;
    7.             rb.angularVelocity = 0f;
    8.            
    9.            
    10.         }
    11.     }
    12.    

    Check my youtube channel

     
    NeonTheCoder, KinanGH and vinimaykaul like this.
  10. maengkom

    maengkom

    Joined:
    Dec 15, 2014
    Posts:
    8
  11. Unrealsentinum

    Unrealsentinum

    Joined:
    Jun 19, 2020
    Posts:
    3
    Thank you so so much i was struggling as well my code was so close to this but i was using AddRelativeForce and my RigidBody was not set up correctly im kinda a noob at this so thank a ton mate
     
  12. vinimaykaul

    vinimaykaul

    Joined:
    Jun 16, 2020
    Posts:
    6
    Thanks This solved my issue.
     
  13. Buchi0

    Buchi0

    Joined:
    Nov 2, 2018
    Posts:
    3
    I know that his is an old thread. But just wanted to say, it did exactly solve my problem (although i am not using a RB, bot moving my character with a CC instead).
    The tip with switching off the gravity that is needed to keep the isGrounded stable during take-off was exactly the right one !

    -Michael