Hello I am trying to make a isGrounded check for jumping for a 2D platformer. My rays come out from the bottom corners of boxcollider2D and their lengths are the half of my collider box towards bottom. I have put a debug with the same parameters to see it it hits anything but when I keep pressing jump it keeps going up and grounded is always true. When I put a layermask to the ground it didn’t jump at all and was never grounded even on the ground. My code is below
Vector2 extent = GetComponent<BoxCollider2D>().size*0.5f;
Vector2 bottomleft = new Vector2(GetComponent<BoxCollider2D>().bounds.min.x, GetComponent<BoxCollider2D>().bounds.min.y);
Vector2 bottomright = new Vector2(GetComponent<BoxCollider2D>().bounds.max.x, GetComponent<BoxCollider2D>().bounds.min.y);
RaycastHit2D groundedleft = Physics2D.Raycast(bottomleft, Vector2.down, extent.y);
RaycastHit2D groundedright = Physics2D.Raycast(bottomright, Vector2.down, extent.y);
Debug.DrawRay(bottomright, Vector2.down * extent.y, Color.green);
Debug.DrawRay(bottomleft, Vector2.down * extent.y, Color.green);
//jumping
if (Input.GetKey("up") && groundedleft.collider !=null && groundedright.collider !=null)
{
grounded = true;
rigid.AddForce(jump);
}
unfortunately this information is not documented, in my experience. i'm unfamiliar w/ the device you mention. but @fortisvenaliter's suggestion is good.
– elenzil