- Home /
How to changing Lense flare intensity based on its distance from the main camera
I have made a 3D space sim game ,I actually know how to get the whole changing of intensity of the lights done . but the calculations and procedures needed to successfully get a smooth fade of the lense flare is extremely bothersome.
so I have my space ship , a light component in the form of a point light is added to the space ships thruster spawn point at runtime , then a lense flare is added to each thruster spawn point.
void Update ()
{
ControlThrusterFlareAndLightIntensity();
}
void ControlThrusterFlareAndLightIntensity()
{
try
{
Thruster.GetComponent<Light>().light.intensity = Vector3.Distance(Camera.main.transform.position,transform.position)/* + Calculation that i need help with*/;
}
catch
{
return;
}
}
initially i had //Thruster.GetComponent().light.intensity = 5-(SpeedOfMyShip/10)
but the change in intensity was far too swift and because the flare never actually changes size the distance my ship moves from the camera became irrelevant . so the flare never got dim as the ship moved away from the camera .
It would be great if someone could help me out with that line of code so that this thing can get working :)
Answer by Pecek · Mar 12, 2014 at 03:08 AM
You can use Vector3.Distance() to get the distance between 2 points(in your case the ship and the camera), after that it's up to you how you want to use that value. I would lerp the intensity based on the distance(and probably clamp the distance so it can be in a certain range).
If you are not familiar with these functions, you can use something like this(it's not tested, sorry if there is any spelling errors)
distance = Vector3.Distance(cam.transform.position, ship.transform.position)
lightIntensity = Mathf.Lerp(0, 10, Mathf.Clamp(distance, 0, 1))
The above script is in boo, translate it to your language, and tweak the lerp/clamp values to get the desired effect.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a light turn on and off with the same button 1 Answer
No shadows on directional and keep on getting this error 1 Answer
Image Effect lighting 0 Answers