- Home /
2D Raycast Layer Mask not working
I've got the following code:
 var layerMask : int = 1 << LayerMask.NameToLayer("Map");
 var hit: RaycastHit2D = Physics2D.Raycast(castPt, -Vector2.up, layerMask);
 if (hit != null) {
     print("collided with " + hit.collider.name + " on layer " + hit.collider.gameObject.layer);
 }
This should make the ray ignore anything that isn't on the layer "Map" (which is layer 10), correct? When I run I get the following log:
 collided with Player on layer 8
why is this happening? Thanks
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by robertbu · Dec 22, 2013 at 10:54 PM
It looks like your are passing the layer mask in for the distance parameter. Here is the signature of the Physics2D.Raycast():
 static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
Your answer
 
 
             Follow this Question
Related Questions
Bitmask/LayerMask used in raycast not working 2 Answers
Is it possible to raycast during edit mode? 1 Answer
Character Controller 2D physics or raycast ?? 1 Answer
Raycast2D not working with LayerMask 1 Answer
How do I use layermasks? 9 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                