- Home /
How to disallow double jumping.
I need to restrict the player's ability to double jump. Right now I have jump height restricted by a timer, I need that timer to set itself to one, or the Jumping() function to turn itself off, if the key is released while the player is in the air. if (Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.L)) {
if (moveHorizontal != 0)
{
timer += Time.deltaTime;
if (timer < jumpTime && isGrounded == true)
{
Jumping();
}
}
}
void Jumping()
{
objPlayer.AddForce(Vector3.up * jumpHeight);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Tile")
{
timer = 0f;
isGrounded = true;
}
}
The first issue I see is that you don't set isGrounded = false, but perhaps that just occurs is another location. So to me, it seems like a player can jump continuously until the timer is greater than the jumpTime.
Answer by Bioinformatizer · Jun 08, 2015 at 05:51 PM
Try adding isGrounded = false; after the line objPlayer.AddForce(Vector3.up * jumpHeight); right now your code doesn't seem to be using is grounded.
Answer by TheXWolf · Jun 08, 2015 at 05:51 PM
As rolling said I don't see anywhere that the player's "isGrounded = false". You would need either OnCollisionExit2D, or depending on how complex your system is you might just want to line/ray cast to a marker that goes below ground. That way it's being check constantly. At the very least you'd want something along the lines of;
void Jumping()
{
objPlayer.AddForce(Vector3.up * jumpHeight);
isGrounded = false;
}
OR
If you're creating a game based on tiles, mark them as Ground in the layer mask and use
isGrounded = Physics.Linecast(transform.position, Grounding.transform.position, 1 << LayerMask.NameToLayer("Ground"));
Your answer
Follow this Question
Related Questions
Shaking physics body container while preventing penetration 1 Answer
overlapsphere to destroy NPCs on exit 1 Answer
Fist Passing Through The Target 2 Answers
Collision impact force 1 Answer