- Home /
How can I stop my player from jumping after double jumps? My player consistently jumping.
public float jumpSpeed = 10f; public float jumpForce = 50f; int jumpCount = 1; public bool isGrounded; bool grounded = false; Rigidbody rb;
bool canDoubleJump;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space) && isGrounded && jumpCount< 2)
{
jumpForce = 360f;
rb.AddRelativeForce (new Vector3(0, jumpForce));
canDoubleJump = true;
}
else
{
if (canDoubleJump == true && Input.GetKeyDown(KeyCode.Space))
{
jumpForce = 300f;
rb.AddRelativeForce (new Vector3(0, jumpForce));
canDoubleJump = false;
}
}
}
void EnableDoubleJump()
{
canDoubleJump = true;
}
}
Where is isGrounded being set? How are you incrementing jumpCount?
isGrounded is set in public class public bool isGrounded;
public float jumpSpeed = 10f; public float jumpForce = 50f; int jumpCount = 1; public bool isGrounded; bool grounded = false; Rigidbody rb;
Right, but what I mean is, what is setting them to true or false after you jump. What is increasing the jumpCount after you jump?
Answer by GamitusLabs · Nov 28, 2018 at 09:42 PM
Here's the logic I would use (pseudo-code)
Update()
{
if(curJumps < maxJumps && !applyJump) // allows for single/double/multi-jump
{
if(KeyDown(Jump))
{
switch(curJumps)
{
case 0:
if(grounded)
{
jumpForce = InitialJumpForce;
applyJump = true;
curJumps++;
break;
}
case 1:
jumpForce = AirbornJumpForce;
applyJump = true;
curJumps = 2;
break;
case 2:
// additional air-jumps hereafter
default:
// catchall
}
}
}
}
FixedUpdate()
{
if(applyJump)
{
rb.AddForce(Up * jumpForce, Impulse);
applyJump = false;
}
}
OnCollisionEnter()
{
if(collider == ground)
{
curJumps = 0;
grounded = true;
}
}
OnCollisionExit()
{
if(collider == ground)
{
grounded = false;
}
}
Answer by mayur7garg · Nov 28, 2018 at 06:30 PM
Make sure that isGrounded
bool is set to false once the player is in the air. And I haven't got what is the use of jumpCount
here.
jumpCount is used to calculate the number of jumps the player has to do, but I don't think it a good use.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Please Help me fix my code (jumping animation playing once) 0 Answers
Triple jumping instead of double 2 Answers
Double Jump Not working 2 Answers
2D double jump doesn't work. 2 Answers