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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Double Jump turns into Triple Jump sometimes

Discussion in 'Scripting' started by Jie_Zhang_2, Jan 2, 2021.

  1. Jie_Zhang_2

    Jie_Zhang_2

    Joined:
    Dec 24, 2020
    Posts:
    5
    I'm using a counter to implement double jump, but it only works sometimes. The character is able to triple jump from time to time.

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     public Transform grounded;
    5.     public LayerMask playerMask;
    6.     public Rigidbody rb;
    7.     bool isGrounded;
    8.     float distToGround;
    9.     int counter;
    10.     void Start()
    11.     {
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if(Physics.OverlapSphere(grounded.position, 0.2f, playerMask).Length > 0)
    19.         {
    20.             isGrounded = true;
    21.             counter = 0;
    22.         }
    23.         else
    24.         {
    25.             isGrounded = false;
    26.         }
    27.  
    28.         if(Input.GetButtonDown("Jump") && counter < 2)
    29.         {
    30.             counter++;
    31.             rb.AddForce(0, 1000, 0);
    32.         }
    33.     }
    34.  
    35.     private void FixedUpdate()
    36.     {
    37.         if (!isGrounded)
    38.         {
    39.             rb.AddForce(0, -50, 0);
    40.         }
    41.     }
    42. }
    upload_2021-1-1_22-12-1.png
    The Player Mask is set to everything except for the character itself.
    Thank you very much for your time and help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    If you think the test in line 18 is hitting something that it should not, revamp the test to return the RaycastHit objects and print their names so you can see what it is you hit.
     
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Yes, an errant hit is probably the cause. Also you can print out the counter values and see, it will probably get reset to 0 unexpectedly.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Is there any thing common among all the time you can triple jump?
    unless you have a bug at the test @Kurt-Dekker mentioned you could also have some colliders extending beyond the renderers.
    if you just walk on a perfect flat plane does it still happen?
    does the delay between each jump matter? if you wait a second before trying to jump again in the air can you still do it or do you gotta do it in succession with a previous jump?
     
  5. Jie_Zhang_2

    Jie_Zhang_2

    Joined:
    Dec 24, 2020
    Posts:
    5
    When triple jump happens, the counter remains at 0 after the first space hit.
     
  6. Jie_Zhang_2

    Jie_Zhang_2

    Joined:
    Dec 24, 2020
    Posts:
    5
    Thanks, I will try that!
     
  7. Jie_Zhang_2

    Jie_Zhang_2

    Joined:
    Dec 24, 2020
    Posts:
    5
    I can't seem to find the pattern of triple jump, I tried to remove all other game objects except for the flat plane and the character, triple jump still happens.
     
  8. Jie_Zhang_2

    Jie_Zhang_2

    Joined:
    Dec 24, 2020
    Posts:
    5
    Problem solved. I think it was the problem of FixedUpdate() and Update(). I tried to move the ground check and jump code to FixedUpdate(), and Update() only checks for if the space button is pressed. So I initialized a bool variable jumped in Updated(), whenever space is pressed, jumped is set to true. And in FixedUpdate(), before applying force to the character, check if jumped is true. I am new to Unity and I'm not sure why this happens, I'm guessing it's because Update() and FixedUpdate() run on different speed, so they need to have an extra condition to align them.
    Thanks for all the helps!