Hello, I’m thinking of including a light detection system in my game that detects if a player is in light or not and will determine if some entities in the game can see you or not
another game I like has a light detection system that uses a hitbox method, and there is another method that involves Maths. I was wondering what the best way to make a light detection system would be and how I’d go about making it? Thank you in advance
I’ve got an idea. First you loop all of the light in workspace. Then you get the distance from a player and the light parent and calculate a distance. Then check if the distance is in a light range (properties range) if it is then say it is visible
It really depends what your game is. If it only requires simple light zones (like flashlights and such) you should probably use hitboxes/zones. Doing the math constantly really should (generally) only be used as a last resort.
Edit:
We could probably help if you gave code or described your use case. You almost certainly shouldn’t use a system like that and instead use zones of some kind.
It’s hard to do something like this precisely, but there are a few less precise options that don’t require thousands of raycast calculations.
One would be the hitbox method you mentioned in which the bounds of the light are within the hitbox. Then just shoot a ray to the touching part to make sure there’s no collision. This is probably the easiest and best solution for most cases.
Another option would be to divide up space into block chunks. Each of those chunks could be given a light level based on the light source(s) and can also take into account if the chunk is behind collision via raycasts. Although with the exception of some very specific use-cases, this is basically a less efficient option to the method above.
Lastly, you could use Minecraft’s lighting system which is similar to the above method, but doesn’t require raycasting due to the detection math being done already with the node environment.
The first method would probably be the best for most situations, so I would recommend that one unless you’re doing something more specific which somehow doesn’t work with that.