- Home /
Lower light intensity with distance and when you are outside certain radius?
Hi, I wanna lower light with distance from an object but I'm having trouble
This is the code I have right now, it kinda works but not exactly as I want it to work
void Update() {
dist = Vector3.Distance(transform.position, signal.position);
flashLight.intensity = (radius - dist).Map(min, top, min, maxIntensity);
}
I know one of the mistakes is that (rad - dist) will be negative at some point so light intensity goes to 0 at some point instead of min
radius was supposed to be what it sounds like, a distance within the light works well but right now I don't know how to exactly implement it, as it is in this moment, the light starts to decrease right after the radius but it does it a little bit fast and it goes completely dark afterwards because of the negative number
Right now, I would want light to decrease as it gets away from the signal position
I've also tried doing something like
intensity = maxIntensity / dist;
but it changes way too fast, I want it to be slower
intensity = maxIntensity / dist.Map(minIntensity, upperIntensity, minIntensity, maxIntensity);
Tried that, it is slower than before but still goes down way too fast
flashLight.intensity = Mathf.Lerp( $$anonymous$$Intensity, maxIntensity , Mathf.Clamp01(dist/radius) );
And if you want full control over it's intensity you may want to use AnimationCurve
instead.
Btw.: If this code starts to appear in Profiler window - make sure it's not executed for object outside of camera view.
Your answer
Follow this Question
Related Questions
Change Lighting of grid base on distance from player. 0 Answers
Why is my terrain dark in the distance? (It gets lighter when moving closer.) 4 Answers
Getting an extremely bright object (the sun) visible from an extreme distance? 2 Answers
Change Lighting of grid base on distance from player. 1 Answer