#Here ya go
1 messages · Page 1 of 1 (latest)
Input.GetKey and Input.GetButton will return true if the button is being held down. You may want to use GetKeyDown and GetButtonDown. I'd also recommend looking into the new Input system rather than the old one.
I'm not sure where you set isJumping to false, but if it's related to collision callbacks, it be getting set to false incorrectly due to other collisions
I've had a problem with using GetKeyDown and GetButtonDown where the player can't hold down the button or mouse to continuously jump when they land, so this is why I chose to use GetKey and GetButton
As for isJumping I'm using OnCollisionEnter2D(Collision2D collision) to set isJumping back to false
don't use onCollisionExit, use raycasts or Physics.CheckBox/Physics.CheckSphere
if you bump into any object with this logic, it will reset the jump even in the air. Something I do is ignore more things while going up in more cases, since you don't want them to reset to grounded until they start falling. That depends on the game though
Just tried this, but it only made the player jumping more inconsistent and it hasn't really fixed the problem
private void FixedUpdate()
{
Vector2 boxSize = new Vector2(GetComponent<BoxCollider2D>().size.x * 0.9f, 0.1f);
Vector2 boxCenter = new Vector2(transform.position.x, transform.position.y - GetComponent<BoxCollider2D>().size.y / 2 - 0.05f);
Collider2D hit = Physics2D.OverlapBox(boxCenter, boxSize, 0f, LayerMask.GetMask("Ground"));
if (hit != null)
{
isJumping = false;
}
else
{
isJumping = true;
}
if (Input.GetKey(KeyCode.Space) || Input.GetButton("Fire1"))
{
if (!isJumping)
{
playerRigidbody.AddForceAtPosition(Vector2.up * jumpForce, transform.position);
}
}
}```
I'll revert it for now so that I can have something with consistent physics, etc. But yea
Think I should probably turn the cubes into a single collider instead of having multiple of them side by side? It fixes the issue but it feels a bit unusual
Generally, you'd want a bit of a more sophisticated approach to jumping in general. Relying on an overlap won't give the best results. I tend to shape cast in the direction of player velocity whenever they are falling. That way you can predict when the player is going to land rather than after the already have.
You can also buffer a jump so that if they player presses the button early (or is holding it down) you can guarantee a frame of the player on the ground and grounded before jumping again
Yea lol, I ended up opting to use a single long collider instead of multiple lined next to each other
This really just is a temporary fix for what I call the "Super Jump" bug but yea, its meant to be more like a personal-ish project that I can have some fun with