- Home /
Light intensity based on player distance
My code below is a bit messed up due to trying one and another but I can't get my idea to work properly.
I have a basic point light in my scene and my script attached to it. The target is the player. The var distance calculates the distance between the light and the player. What I want is that the light is off if the player is > 30 away. If the player gets close, the light must shine more brighter and if I walk further away it must dim.
How could I achieve this?
var Target : Transform;
var distance : float;
var maxIntensity : float = 3.0;
function Update() {
distance = Mathf.Round(Vector3.Distance(gameObject.transform.position, Target.transform.position));
if(distance>30){
light.intensity = 0.0;
print ("light off");
}
if(distance<30){
light.intensity += Time.deltaTime * distance;
if(light.intensity == 0.0){
light.intensity = 0.1;
}
}
if (Random.value > 0.9 && light.intensity > 0.1) {
StartCoroutine("FlickerLight");
}
}
function FlickerLight() {
while(light.intensity > 0.0) {
light.intensity -= Time.deltaTime * distance;
yield;
}
while(light.intensity < 1.0) {
light.intensity += Time.deltaTime * distance;
yield;
}
light.intensity = 1.0;
}
Tell me if I'm wrong, but reading your code, even if I am at 29 meters, the light will get brighter and brighter, right?
Answer by robertbu · Aug 11, 2013 at 08:44 PM
For your specific question, not dealing with the flickering lights, you can do:
if (distance > 30) {
light.intensity = 0.0;
}
else {
light.intensity = (1.0 - distance/30.0) * maxIntensity;
}
Where 'maxIntensity' is a variable you define and initialize or you can replace it with a hard-coded value. According to the reference, Light.intensity (and therefore 'maxIntensity) must be set in range of 0 to 8.
Almost perfect! Your code works, but it kind of stutters if you get what I mean. How can we make the intensity of the light change more fluently?
Indeed, should be pretty straightforward. You can make things a little more complicated but using the value from robertbu's code as a targetvalue, and add/remove value from your intensity each frame.
Which would look more like what your code was originally meant to do, or that would be my guess anyway.
You can use $$anonymous$$athf.Lerp(). $$anonymous$$erge this into your code:
var intensity : float;
var speed : float = 3.0;
var maxIntensity = 8.0;
function Update() {
if (distance > 30.0) {
intensity = 0.0;
}
else {
intensity = (1.0 - distance/30.0) * maxIntensity;
}
light.intensity = $$anonymous$$athf.Lerp(light.intensity, intensity, speed * Time.deltaTime);
}
'speed' will deter$$anonymous$$e how quickly the light will adjust. This code will produce an easing of the light to its final intensity. For a non-eased look, use $$anonymous$$athf.$$anonymous$$oveTowards() ins$$anonymous$$d of $$anonymous$$athf.Lerp(). You will have to use a different range of values for 'speed' if you change to $$anonymous$$oveTowards().
Your answer
Follow this Question
Related Questions
How to make a point light which flashes when a key is pressed? 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Random Array of sounds for Random Game Object 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Animation for weapon plays on game start instead of when i attack 2 Answers