0

I want to know if an object is currently "lit", so I want to detect if the light source hits it.

I thought of a trigger as big as the light but I think that it would be quite annoying to put a specific collider for every light (I would need to modify the collider to adapt it to other light "shapes"). Is there a way / a better way than mine?

1 Answer 1

1

Yes, your way is probably the best way depending on how accurately you want to detect if an object is lit.

  • If the light cannot be blocked by other objects and the check does not have to be very precise, you can just use the Vector2.Distance method to see whether the object's center is within Light.range of the light's center.
  • If the light can be blocked by other objects, but the check still does not have to be very precise, you can cast a Raycast from the object's center to the light's center and check against Light.range again.
  • If the check does have to be precise, the best way is indeed probably just using a collider on your light. If you have many colliders in your scene, I would recommend creating two new layers: one to which you then assign all the 'touchable' lights, and another for the objects that can be 'spotted' by those lights. You can then go to Edit > ProjectSettings > Physics and in the Layer Collision Matrix, for the light layer, only tick the interaction with your 'spottable objects' layer. That way Unity won't do unnecessary collision checks with other objects.
  • If you need to be precise to the pixel, there might be a solution with shaders for that, but that would (at the very least) not be so easy to implement and could be very inefficient if used for many objects and lights.

If you have many different, or even randomly generated, light shapes, you could create a script that, in the Awake method, sets the collider's size/shape automatically.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! You said: "If you have many different, or even randomly generated, light shapes, you could create a script that, in the Awake method, sets the collider's size/shape automatically.", can you explain how to code it? I'm not a very good gamedev and I've never used Awake before.
The Awake method is similar to the Start method, but is called even before the Start method. How you code the collider generation script depends entirely on what shape you want the collider to be. You could either use a PolygonCollider2D to define the collider from a 'path', or you could use a CustomCollider2D to define the collider using primitive shapes (also including polygons). You can reference the Unity Docs to see what methods you need to call: docs.unity3d.com/ScriptReference/CustomCollider2D.html and docs.unity3d.com/ScriptReference/PolygonCollider2D.html
Also, if you just meant differently sized point lights, not really any exotic shapes, you could make the script to set the range of the CircleCollider2D to be the same as the range of the light, for example. And if you are using a freeform light, you can simply 'recycle' the path of the light for the path of the PolygonCollider2D. This forum post shows how to get (and/or set, if you want to generate the light shapes as well) the polygon path of the freeform light: forum.unity.com/threads/2d-light-edit-shape-from-script.804033
Thanks a lot for being so precise, I was thinking of a "cone" like shape, but I don't really know how to do it (I haven't search the things you suggested yet). I was also thinking that maybe the best solution to detect if an object is "lit" is using both a ray cast from the object to the light (as you suggested) and also the collider to see if it's in the "light range and shape".
I'm not sure what kind of shapes your objects are and what your scene looks like, but how I interpret what you are trying to achieve is: objects can be behind walls, but within the light range and shape, for which you then cast the ray to check if it is actually lit and not behind a wall. If so, that solution would be good I think. You can create a cone shaped light in 2D using the Point light type of Light2D and changing the angles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.