Hey everyone! I come ask for advice for an issue with my ground detection for a 2D project (I'm new to unity) So, i basically made a jump and use a transform with a radius of 0.06 under my character to detect the ground under it. It works fine most of the time, however, if a very specific case i have some troble with "steps" If i touch the corner of that step, it actually reset my boolean "isGrounded" allowing the character to make another jump. So i wanted to know if there is a better way to detect ground for 2D game. Or perhaps if i just need to adjust my code detect that case in particular. Here's my code, btw i'm learning how to script so all advices are welcomed! Code (CSharp): public class PlayerControler : MonoBehaviour { public float speed; public float jumpforce; private float moveInput; private Rigidbody2D rb; private bool facingLeft = true; public bool isGrounded = false; public Transform groundCheck; public float checkradius; public LayerMask whatIsGround; private float jumpTimeCounter; public float jumpTime; private bool isFirstJumping; private bool hasJumped; public bool mutationDoubleJump; private bool isSecondJumping; public float jumpforce2; private bool canCoyote; public float coyoteTime; private float coyoteTimeCounter; public float gravityScale; public float addedGravity; public float PourcentInertie; public int hasBeenGrounded; private bool lastGroundedState; void Start() { mutationDoubleJump = false; rb = GetComponent<Rigidbody2D>(); gravityScale = rb.gravityScale; } private void Update() { //On vérifie si le joueur touche le sol dur if (lastGroundedState != isGrounded && isGrounded == true) hasBeenGrounded++; if (canCoyote) { if (coyoteTimeCounter<=0) { canCoyote = false; } else { coyoteTimeCounter -= Time.deltaTime; } } //Saut simple if (isGrounded == true && Input.GetKeyDown(KeyCode.Space) || canCoyote == true && Input.GetKeyDown(KeyCode.Space)) { isFirstJumping = true; jumpTimeCounter = jumpTime; rb.velocity = Vector2.up * jumpforce; hasJumped = true; } //Saut à nuances if ( Input.GetKey(KeyCode.Space)&& isFirstJumping==true) { if(jumpTimeCounter>0 ) { rb.velocity = Vector2.up * jumpforce; jumpTimeCounter-= Time.deltaTime; hasJumped = true; } else { isFirstJumping = false; } } else if(isFirstJumping == false && mutationDoubleJump == true && Input.GetKeyDown(KeyCode.Space)&& isSecondJumping==true && hasJumped == true) { rb.velocity = Vector2.up * jumpforce2; isSecondJumping = false; rb.gravityScale += addedGravity; } if(Input.GetKeyUp(KeyCode.Space)) { isFirstJumping = false; } if(isGrounded == true) { isSecondJumping = true; canCoyote = true; hasJumped = false; coyoteTimeCounter = coyoteTime; rb.gravityScale = gravityScale; } lastGroundedState = isGrounded; Thanck you for your time. I also apologyze for the poor english
In-case it still helps, I maintain a GitHub project that demonstrates 2D physics features. One example shows how to use existing contacts for a simple IsGrounded state. You can also differentiate angles to detect floor/walls etc and no queries (no raycast etc) are involved. That test scene is here. Note, there are branches for earlier versions of Unity. Note that the set-up I demonstrate reduces the IsGrounded to a simple property shown here. This is that specific scene that demonstrates beyond a certain angle is not counted as grounded i.e. the slope:
It might be helpful for the community if you told how you solved the problem. After all, you posted your question here.