Player Jumps twice with double jump deactivated
Hey guys! I've got a bit of a problem that I just can't figure out how to solve. I'm making a new character controller script and I'm planing an having a double jump in my game when it is unlocked. I'm using Rigidbody.AddForce so I can make another script working (not relevant here).
Everything works so far except that the player can jump twice even though double jump is not active. Here's the relevant code:
if(grounded == true)
{
maxVel = 15.0f;
timesJumped = 0;
}
else
{
maxVel = 7.5f;
}
if(Input.GetKeyDown(KeyCode.W) && timesJumped < maxJumps)
{
Debug.Log("Check " + timesJumped);
timesJumped++;
rBody.AddForce(gameObject.transform.up * jump, ForceMode.Impulse);
}
The problem seems to be, that when I hit W to fast after jumping once, that it doesn't increment "timesJumped" fast enough and because of that the game executes the code a second time... The even stranger thing about that is, that "timesJumped" increments only once even if the player jumps twice. I've even tied using a Coroutine to account for this "lag", but nothing works. The player is able to jump twice regardless.
If you need more information's, just ask and I will provide it ^^
Thanks in advance!
Answer by Geometrical · Oct 09, 2016 at 11:59 PM
The code seems logically correct to me, but perhaps if you provide more information I'll be able to help. Specifically:
How are you checking whether the player is grounded or not?
In which method are you executing this code?
The grounding is checked by another script that is attached to an empty child object with a Collider. It uses OnTriggerEnter to set it to true in case of a collision with the ground and if the player has enough Y-distance to the ground, then it set's it to false again. (That is literally the only thing that script does)
Everything I've posted in my question is executed in the Update function.
Thank you for helping ^^
You should never put any physics related code in the Update function, use FixedUpdate ins$$anonymous$$d. Try it and report back the results.
Just tried that, but I get the same results... But thanks for the info, anyway ^^
Imma go to sleep now. It's late over here. I'll check back tomorrow.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094327im_/https://answers.unity.com/themes/thub/images/avi.jpg)