fade in/out a light when the camera is looking at it + in range + no objects are in the way.

Hey guys,

I am making a game with a big city and many dynamic lights.

To save performance I am making a script to disable and enable lights when you get in close proximity + no object is in the way.

I woud like it so that the script also only executes when the actual light is seen by the camera

So far I have this:

var fadeLights = 0.00;
   
function Update () {
distancevar = Vector3.Distance(transform.position, target.position);
var hit : RaycastHit;
	if(Physics.Linecast(transform.position, target.position, hit)){
		if(hit.collider.gameObject.tag == "PLAYER" && distancevar < range && Tod.lightsOnOff == 1){
			this.light.enabled = true;
			if(this.light.intensity < 1){
			this.light.intensity += 10*Time.deltaTime;
				}	
			}
		else{
		if(this.light.intensity <= 0){
		this.light.enabled = false;
				}
		if(this.light.intensity > 0){
		this.light.intensity -= 10*Time.deltaTime;
				}
			}	
					
		}
}

Do you guys have an idea what I need to add to the first if statement to achieve this?

PS, I am quite new at unity so please if you can explain it fully.

Hi, from what i understand, you put this script on every light right? And you got everything but the angle measurement?

If that’s it then use Vector3.Angle(player.forward, (player.position - transform.position)) to check if the angle if inferior to FoV of the camera.

Ok I found a “not so elegant” solution!

Seeing how the lights disable when an object is in the way, I made a 3 small planes around the camera(right,left, back)
So when you aren’t looking at a light, it gets disabled.

I couldn’t get the vector3 to work.

Thanks anyways!