I have a 2D Point Light in the scene, and a GameObject with a ShadowCaster2D script attached, they are all from Unity URP.
How can I detect if the GameObject is being hit by the point light and thus casting a shadow? Sometimes even the GameObject is within the area of the point light, there can be no shadow because the light might be blocked by other gameobjects, so I can’t just culculate the distance and the area.
I don’t see any events on ShadowCaster2D or Light2D that can call when it happens, and when it happens, I also want to know which point light is lightening the gameobject.
In this case you can use a raycast, but you might need to adjust as this will only do line of sight, not accounting for the sizes of other shadow casters etc.
Every shadow caster will need a collider of the same shape, then cast a ray filtered to the physics layer your shadow casters are on.
You can then use simple check.
RaycastHit2D hit = Physics2D.Raycast(light.transform.position, direction, direction.magnitude, 1 << LayerMask.NameToLayer("Light Detection"));
if(hit.collider == null){
if(hit.collider != colliderInQuestion){
//it's covered by another shadow caster
}
}
If there’s no API of the 2D light that can trigger some events that are needed in this case, then maybe I should cast several rays from the source of a point light so that I can tell if a circular sector light is covering a gameobject that is partly behind another. That’s a very useful clue, thanks a lot for the reply.