RaycastHit2D ground detection
Hi, Im making a 2d platformer game where the player can jump once and if he kills something midair he can jump again. Now im using RaycastHit2D to check if the player is grounded but now I have the problem that somehow the player can jump twice without killing anything midair.
Im trying to solving this problem for a long time now and have no idea how to fix it. Does anyone have a idea how to fix this?
here is the part where I check the ground:
private void CheckGround() {
// Mid Air Control
RaycastHit2D groundRay = Physics2D.Raycast ( this._transform.position, -Vector2.up, 1F, 1 << LayerMask.NameToLayer("Ground") );
// Grounded
if ( groundRay ){
isMidAir = false;
anim.SetBool("isMidAir", false );
jumpEnabled=true;
}
// Not grounded
else{
isMidAir = true;
anim.SetBool("isMidAir", true );
}
}
Answer by sefhi · Nov 20, 2015 at 02:08 PM
Probably can jump twice because the width of the ray. Why don't you try using Physics2D.OverlapCircle ? Just create a little empty gameobject, set it below the character where you want to check if it's grounded, set a radius and use the same LayerMask you're using with the raycast.
Your answer
Follow this Question
Related Questions
2D How to reflect a raycast with a line renderer 0 Answers
Checking for Raycast distances not working as expected. 0 Answers
How can I detect which game object was clicked? 0 Answers
I think my raycast detects destroyed gameobjects 0 Answers
Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 0 Answers