I've been trying to create a double jump using if statements to check if my player is in the air or not to allow the double jump to happen, but I can't find the problem and There is no error in my code and I'm having a logic error. I'm just starting to code so sorry if my code is long and unnecessary.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UIElements;
public class movementscript : MonoBehaviour
{
public Rigidbody2D mybody;
public bool grounded = false;
public int jumpCount;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W) && grounded && jumpCount == 0) // allows jumping to be enabled if character is grounded
{
mybody.velocity = Vector2.up * 10;
jumpCount += 1;
}
else if (Input.GetKey(KeyCode.W) && jumpCount < 2 && grounded == false) // makes the jump count 2 so that a double jump is possible
{
jumpCount += 1;
}
if (jumpCount == 2 && Input.GetKeyDown(KeyCode.W)) // 2 is the number of space bar clicks in order to create a double jump, 1 is the first inital jump, 2 is the second jump which activates the double jump
{
mybody.velocity = Vector2.up * 10;
jumpCount += 1;
}
else if (jumpCount > 2) //returns the number to 0 so that it doesnt continue on where double jumps arent allowed...
{
grounded = false;
jumpCount = 0;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
grounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
grounded = false;
}
}
ive tried a jump checker to see how many times the player jumps to keep track on what jump the player is on (first or second jump and no more) but it seems like it only helps me and not my code
First and foremost, I would create a Jump
method so that your Update method isn't filled with if statements. This Jump
method would be called everytime the player press W and the conditions would be checked inside this method.
Then, the boolean grounded should only be modified by the collision detection so you should not set it manually to false as you do in if (jumpCount > 2)
. It's the same for reseting jumpCount to 0, that should be handled by the CollisionEnter event beacause it's when the player touch the ground that he has the ability to jump again.
Here is what I would write :
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UIElements;
public class movementscript : MonoBehaviour
{
public Rigidbody2D mybody;
public bool grounded = false;
public int jumpCount;
// Start is called before the first frame update
void Start()
{
mybody = GetComponent<RigidBody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Jump()
}
}
private void Jump()
{
if(grounded)
{
mybody.velocity = Vector2.up * 10;
jumpCount = 1;
}
else if (jumpCount == 1)
{
jumpCount = 2;
}
else if (jumpCount == 2)
{
mybody.velocity = Vector2.up * 10;
jumpCount = 3;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
grounded = true;
jumpCount = 0;
}
private void OnCollisionExit2D(Collision2D collision)
{
grounded = false;
}
}
In order to avoid further misbehavior, you should also check if the collider your player enter in collision with is the ground to reset grounded. Else, if he touches a projectil or whatever in the air, he would be able to jump twice again in the air. To check if the collider is the ground, add a Tag or a layer on you object and check that in the OnCollisionEnter2D. You can find a lot of tuto explaining this beter than me on internet