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;
}
}