Simple Light Index Mechanic For Stealth Games

I recently picked up all the old Splinter Cell games after finding they were all backwards compatible with Xbox One and was thinking of a really easy way to implement a light meter. I knocked up a really simple solution in just over an hour and it can act as a nice jumping-off point.

I google-searched “amount of light on gameobject in Unity” that came up for a variety of ideas, some of which involved raycasts, some of which involved getting pixel colours. I opted for a really simple Triggers solution as this would be flexible for designers if they wanted to tweak light areas without depending on the actual lighting in the scene itself.

I set up this scene, putting in a spotlight and grabbing some animations from Mixamo. I wrote some movement and animation code, added a character controller and rigidbody to the character (because we are using Triggers).

I added a child game object to my Spotlight and added a Capsule Collider component, setting it as a trigger:

I created a Light Index component and placed it on the player:

This was going to keep track of our Light Index, but also show the transforms to check. In my component, I hooked up parts of my character’s body:

Then I created the Light Collider component and added it to my light collider I set up before hand:

Firstly the script checks if the player intersects the collider and then cycles through the serialized transforms to see how many are actually within the collider using a distance calculation. The percentage of colliders inside the collider then provide the light index as seen below:

Like I said before, this is more of a jumping-off point as right now, this implementation only works with Capsule Colliders and circular lights.

If you are using a Box Collider, instead of using the radius check you could use Unity’s Bounds.Contains to see how many of the points are inside the box collider

For more complex meshes, it depends on how you would want to use the system.

Assuming you are using 2D shapes on the ground and just checking the players X and Z, you could loop through the vertices and check if the TransformsToCheck points X and Z value are within the bounds.

If you wanted to include the Y and take the overall Volume of a mesh into account then you could use the following method to check if the point is inside:

https://answers.unity.com/questions/611947/am-i-inside-a-volume-without-colliders.html

I hope this all helps! If you want to see the Unity project for the basic implementation then you can take a look on github:

Leave a Reply

Your email address will not be published. Required fields are marked *