- Home /
problems with grounded and double jumping
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;
}
}
Hello! Can I ask about your decision to use Physics2D.OverlapCircle for checking if the player is grounded? What object does groundCheck refer to?
I was using a tutorial on jumping and didn't pay much to it. I thought it had something do with the layer mask I made called ground in the tutorial. Can you give more detail what it does because I've been stuck at this for a couple hours?
It's not necessarily wrong. I just don't know what your game is like, so maybe it's the best choice. Physics2d.OverlapCircle just checks to see what colliders fall within a circle. The circle is located at groundCheck.position in your code. I was just curious what object that refers to, is all.
Answer by hrishihawk · Mar 15, 2018 at 05:58 AM
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.
thank you for the advice it fix the infinite jumping but I can't mange to make character double jump.
Ok I looked through your script, found an issue.Get rid of this :
if (grounded)
{
doubleJump = false;
}
and add doubleJump = false here :
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space ) && !grounded && doubleJump)
{
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
doubleJump = false;
}
@BlazeCrow and it's better do all the physics in FIxedUpdate.
Answer by Harinezumi · Mar 14, 2018 at 03:44 PM
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).
nah it didn't work but as I as I jump the doubleJump is checked and I can't jump after that.
Your answer
Follow this Question
Related Questions
About character doublejumping. 1 Answer
Double Jump using First Person Controller 1 Answer
Jumps never reset 2 Answers
Jump of 2d character is sometimes stronger. 1 Answer
Double jump does not work why? 0 Answers