Unity Edge Detection issue

41 Views Asked by At

So for a couple of days now I've been stuck trying to fix my edge detection in unity. You see what happens is when I'm under the edge and i let go of the ctrl button and leave from the edge the player is stuck in the edge detection cycle until I press ctrl again which the player is returns to there normal player state. I've Supplied the in Pastebin here is the link: https://pastebin.com/r6GWw4ba

private bool IsUnderEdge()
{
    RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, EdgeCheckDistance, groundLayer);
    return hit.collider != null;
}

`. I for got to mention that the crouching colliders don't switch either its also stuck in the edge detection state. Some more detail I have a empty GameObject for overhead that im trying to use.

Keep in mind im still "Baby" coding in game development I've tried google Gemini and Chatgpt and they kept braking my code with animations or the edge detection or even both at the same time.

1

There are 1 best solutions below

0
Bodzik On

So I i just removed Input.GetKeyUp and just used the condition of !isEdgeAbove and changed the stated based if there was an edge above or no.

 private void PlayerCrouching()
 {
 if (Input.GetKey(KeyCode.LeftControl) && isGrounded)
 {
     SetCrouch(true);
 }
 else if (!isEdgeAbove) 
 {
     SetCrouch(false);
 }
 if (isCrouching == true)
 {
     PlayerAnimator.Instance.ChangeAnimationState(PlayerAnimator.Player_Crouch);
 }
 

}