Object is lit - how to do

I have a scene with some PCs and NPCs and a want a gameplay which revolves around PCs and NPCs seeing each other, with sight status being calculated based - among other things - on a unit being lit by one of the scene’s light sources (basically either dark, or far, or linecast = doesn’t see).

At the moment, i’m basically using point lights and a combination of range check (through Vector3 magnitude) and line of sight check (linecast against a layer mask) from the position of point light to the position of a character. Problem is, this doesn’t take advantage of Unity’s lighting system at all. I was wondering if there was a more direct way to see if a character is lit by any of the scene’s lights.

The scene atm is using point lights (and an overall directional light, which doesn’t influence game logic). But i’m also considering the inclusion of spot lights.
The idea is to make a game for mobile platforms, which means efficiency is an issue.

Code for current implementation of light checker below.

using UnityEngine;
public class USP_Light : MonoBehaviour {
	private static USP_Light[] arr;	// holds static references to all objects of this type
	private float range;	// grabs range of light component
	private Vector3 loc;	// grabs transform.position of light component
...
	public static bool InLight(Vector3 p) {
	// checks if a specific unit is affected by one of point lights
	// called once per character on scene per update
		foreach(USP_Light l in arr) {
			if (((p-l.loc).magnitude < l.range) 
				!Linecast(p, l.loc, sightBlockLayerMask))
				return true;
		}
		return false;
	}
}

You could try a system where you find the distance between the object you want to have the lights levels read for and the point lights position. Then just divide that distance by the range of the lights divided by two (because if you imagine a circle, the range would be a diameter, I think not sure how Unity does it) so you get a normalized value that can be multiplied by the lamp’s intensity to get a rough idea of what the light levels where the object is might be. This is a really basic linear falloff, you could get fancy and do some sort of curve function or something, but I think for the purpose of keeping it simple it works :smile:

I hope this helps, PM me if you have any questions, I’m not too active on the forums so it might be awhile until I get back to you