- Home /
2D Raycast not detecting right layer
I got a GameObject like so: main GameObject with rigidbody2D + colider2D that has 3 child, last one got script called ShadowAffectedByLantern.
The Player script throw Raycast2d like so:
private void LanternAffectShadow()
{
var hit2D = Physics2D.Raycast(transform.position, _direction, Mathf.Infinity, 11);
if (!hit2D.collider) return;
Debug.Log(hit2D.collider.gameObject.name);
var shadowAffectedByLantern = hit2D.collider.gameObject.GetComponentInChildren<ShadowAffectedByLantern>();
if (!shadowAffectedByLantern)
{
return;
}
shadowAffectedByLantern.SetShadowTransform(
hit2D.distance,
Quaternion.AngleAxis(_angle * Mathf.Rad2Deg, Vector3.forward)
);
}
Basically, I try to execute that child script throw his parent but somehow raycast hit take the parent that is not event the layer mask I try to hit.
Comment
Btw, Debug.Log(hit2D.collider.gameObject.name); Return the player that throw Ray.
Best Answer
Answer by YaGa · Feb 08, 2021 at 09:41 PM
Make sure you're getting the LayerMask and not the layer, they are different things. easiest way to get layermask is
LayerMask.GetMask("Your Layer name")
as the last parameter in you Raycast For more info: https://docs.unity3d.com/ScriptReference/LayerMask.html