- Home /
RayCast2d Not colliding with objects in other layers
I have the following code. My player and certain objects are in PlayerAndEnemies. I have a tilemap2d (which is what I want the raycast to hit) but it does not collide with it. The tilemap 2d does have a tilemap collider and is in other layers (such as default, and others I created). Can anyone point me in the direction for why this would not collide?
Movement movement = player.GetComponent<Movement>();
RaycastHit2D hit;
int layerMask = LayerMask.GetMask("PlayerAndEnemies");
//layerMask = ~layerMask;
if (movement.directionFacing == 0)
{
hit = Physics2D.Raycast(player.transform.position, Vector2.down, 100f, layerMask);
} else if (movement.directionFacing == 1)
{
hit = Physics2D.Raycast(player.transform.position, Vector2.up, 100f, layerMask);
} else if (movement.directionFacing == 2)
{
hit = Physics2D.Raycast(player.transform.position, Vector2.left, 100f, layerMask);
} else // 3
{
hit = Physics2D.Raycast(player.transform.position, Vector2.right, 100f, layerMask);
}
//Debug.DrawRay(hit, Color.red);
//Check distance of raycast
print(hit.distance);
print(hit.collider);
Answer by WarmedxMints · Mar 02, 2019 at 09:05 PM
A raycast looks for gameobecjts. If you have specified a layermask, then it will only look for objects selected in that mask. A gameobject can only be in one layer. You would need to put the gameobject with the tilemap contains the tiles you wish to raycast against on a layer which is in your desired layer.
Your answer
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
What is GraphicsRaycaster.Raycast results order? 0 Answers
Ray is not being detected 0 Answers
Raycasting completely cancelling instantiation. 1 Answer
[help]RayCast check [SOLVED] 2 Answers