- Home /
2D Raycasting For Ground/Ceiling Checks
I've researched everywhere trying to figure this out but I can't wrap my mind around it.
I want to know when the player touches the ground or has ceiling above it (when crouching). And I'd like the ray to be visible in scene view.
I thought I'd do this:
 private BoxCollider2D collider;
 private int mask;
 private float distToGround;
 
 void Awake ()
 {
      collider = GetComponent<BoxCollider2D>();
      mask = ~(LayerMask.GetMask ("Platforms"));
      distToGround = collider.bounds.extents.y;
 }
 
 void GroundCeilingCheck ()
 {
      Debug.DrawRay (transform.position, -Vector3.up, Color.red, distToGround);
      if (Physics2D.Raycast (transform.position, -Vector3.up, distToGround, mask)
      {
           isGrounded = true;
      }
      else
      {
           isGrounded = false;
      }
 }
But that didn't cast a ray at all.
Any help would be greatly appreciated!
Answer by GamitusLabs · Oct 24, 2018 at 05:10 AM
Why not just use the players OnCollision? Just check to see if the collision is at its feet.
I did until I tried to implement more functionality such as sliding. So now I need ground and ceiling checks and I feel it'd be cleaner and more efficient to raycast ins$$anonymous$$d of dealing with ground and ceiling colliders
Your answer
 
 
             Follow this Question
Related Questions
RaycastHit2D does not work? 0 Answers
How to use 2D box colliders with out Rigidbody2D 0 Answers
Is it possible to raycast during edit mode? 1 Answer
Is Physics2D.raycastsStartInColliders new? 0 Answers
Raycast2D Help 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                