I mange to work with the regular jumping but when I implemented the double jump then it started to act funny. I can only jump once and as soon I get rid of double jump it can now infinite jump. I can’t seem figure hot to stop the infinite jump and how I got to work working right in the first place.
public class PlayerPlatformController : MonoBehaviour {
public float topSpeed = 10f;
bool faceToRight = true;
public bool grounded = false;
public Transform groundCheck;
public LayerMask whatIsGround;
public float groundRadius;
public float jumpVelocity = 7;
public bool doubleJump = false;
void FixedUpdate() {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !faceToRight)
Flip();
else if (move < 0 && faceToRight)
Flip();
}
void Update() {
if (grounded)
{
doubleJump = false;
}
if (Input.GetKeyDown(KeyCode.Space ) && grounded && doubleJump)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
}
if (Input.GetKeyDown(KeyCode.Space) && !grounded && !doubleJump)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
doubleJump = true;
}
}
void Flip()
{
faceToRight = !faceToRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
@BlazeCrow
Your first problem is that your boolean variable grounded
is never set to true.Which means your ground check condition fails for some reason. It’s probably happening because your groundRadius
value is too small. So try changing your groundRadius
variable and check if grounded
has the expected value. Your first step is to make sure grounded
variable actually set to true when the character is on the ground before moving on to implement jump and double jump.
Even if you get the ground checking to work right your script won’t work as expected because you have got your conditions for jumping and double jumping wrong.
First let’s solve jumping :
You want your character to jump when the following conditions are met
- If the character is on the ground ( when
grounded = true
)
- If the user presses Space key ( when
Input.GetKeyDown(KeyCode.Space)
)
Now let’s do the same for double jump :
Your character can jump again if the following conditions are met :
- If the character is in air ( when
grounded = false
)
- If the user presses Space key ( when
Input.GetKeyDown(KeyCode.Space)
)
- If
doubleJump = true
Now using conditions for both jumping and double jumping your script becomes :
if (Input.GetKeyDown(KeyCode.Space ) && !grounded && doubleJump)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
}
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
doubleJump = true;
}
If you still got any doubts feel free to comment.
Maybe the problem is that FixedUpdate()
is called less frequently (every 0.02 seconds) than Update()
(every frame), and you set grounded
in FixedUpdate()
, but use it to set doubleJump
to false
in Update()
.
Try moving the line grounded = Physics2D.OverlapCircle(...);
to the beginning of Update()
, and see if that helps (I know that it is part of physics and probably uses distances, but it’s just a circle overlap, it won’t have that much impact on performance).