- Home /
Jumping when not grounded
My character seems to jump when not grounded. I can jump without being o the ground. I have created a ground check and a layer mask (so that the player can only jump off the floor).
However in my code the I have made a debug log saying "grounded" every time I am on the ground. But for some reason I can jump even when I am not grounded (no messages appear in debug log while I am continuously jumping). I should not be able to jump but I can.
The Player and the ground check are on a "Player" layer and the floor is on a "Floor" layer. whatIsGround is a public layer mask which uses only the "Floor" layer.
void Update ()
{
if (Input.GetButtonDown ("Jump") && grounded) // "Jump" is the spacebar key on the keyboard
{
if(grounded)
Player.AddForce (new Vector2 (jumpSpeed, jumpForce));
// makes the character jump
}
and in fixed update
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
if (grounded)
Debug.Log("grounded");
It doesn't make much sense to me my code seems perfectly fine (these are just snippets of the code that refer to grounded and the jumping problem)
Hmm,, try taking out that extra bracket statement that says if(grounded) inside the Input if statement.
I don't think you'll need that since you'll only be adding force if you're grounded and you press the Space button.
Answer by Fappp · Apr 24, 2015 at 10:02 AM
At first glance it looks to me grounded is never set to false. Why did you decide to go with this approach?
The way I would tackle this:
Give everything walkable the tag Floor
Create a 2D boxcollider on the player thats marked as trigger.
If the trigger enters > grounded = true;
If the trigger exits > grounded = false;
I use this approach for basically any action I want to perform with collisions because it avoids the Update function in most cases. ( In this particular case you're bound to update because of Rigidbodies )
Please also note having a boxCollider as trigger doesn't interfere with your physics, raycasts or world collision, it's only for triggering purposes.
How would I make it so that the collider only triggers when it hits the floor tag?
Here's the JS I've used for the demo:
#pragma strict
var grounded : boolean;
var rb : Rigidbody;
function Start(){
rb = gameObject.GetComponent(Rigidbody);
}
function Update(){
if ( grounded ){
Jump();
}
}
function Jump(){
rb.AddForce( Vector3.up * 100 );
}
function OnTriggerEnter ( other : Collider ) {
if ( other.gameObject.CompareTag("Floor")){
grounded = true;
}
}
function OnTriggerExit ( other : Collider ) {
grounded = false;
}
Conclusively you should test the tag of the object hit, as in:
if ( other.gameObject.CompareTag("Floor")){ // Perform action }
Answer by Sondre-S · Apr 24, 2015 at 10:28 AM
I usually do a raycast to detect if the player is on the ground.
// distance to ground
var hit : RaycastHit;
if (Physics.Raycast (Character.position, -Vector3.up, hit,100)) {
distanceToGround = hit.distance;
}
if(distanceToGround>0.6){OnGround=false;}
if(distanceToGround<0.6){OnGround=true;}
//jump
if(Input.GetAxis("Jump")&&Key && onGround && JumpTimer<0.1)
{
VerticalSpeed= 0.3;
Jump=true;
Key=false;
}
if(!Input.GetAxis("Jump")&&!Key){Key=true;} // check if key is already pressed
if(distanceToGround>3)
{
VerticalSpeed= 0;
}
if(Jump){
JumpTimer+=0.01;
}
if(JumpTimer>JumpTime)
{
Jump=false;
JumpTimer=0;
}
///// Move character
Character.Translate(SidewaysSpeed,VerticalSpeed,ForwardSpee;
Raycasting is too heavy on performance. Check this: http://fb-projecten.com/stress/ Press E to switch between ray and trigger
Your answer
Follow this Question
Related Questions
How to fix jumping from Unity2D tutorial? 2 Answers
Unity ignoring bool grounded 1 Answer
Jump of 2d character is sometimes stronger. 1 Answer
2d grounded check problem? 3 Answers
Unity 2D Platformer: How to properly implement this ground check? 1 Answer