- Home /
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.
Answer by ks13 · Dec 19, 2011 at 04:16 PM
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.
You can also use the built in camera functions to see if the light is on-screen. Take a look at http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToViewportPoint.html - to check if the point is on screen, check that the x&y are within (0,1) and the z > 0 on the transformed Vector3.
Answer by PuttPutt · Dec 19, 2011 at 10:53 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613062214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Know if gameObject is being seen 2 Answers
Camera Icon 1 Answer
How to make camera position relative to a specific target. 1 Answer
v2019.4 Gizmos NOT VISIBLE 0 Answers
Third person camera and Object 1 Answer