Fade Light in OnTriggerEnter and Fade Light out OnTriggerExit
Hey all,
Trying to get a light to fade in and out when the player enters and exits a boxcollider (is Trigger is selected). I took some stuff from here https://unity3d.com/learn/tutorials/projects/stealth/alarm-lights but am getting a lot of flickering when I attach this script to the box collider gameObject. I can tell something is happening when I enter and exit the collider but looks terribly messed up. I can get the light to turn on and off with no problem but when I introduce the lerping and if statements I get thrown off. Any ideas? I imagine I'm doing something really dumb here but i'm but a lowly n00b
using UnityEngine;
using System.Collections;
public class fadelight : MonoBehaviour {
public Light light;
public float fadeSpeed = 2f;
public float highIntensity = 6f;
public float lowIntensity = 0.0f;
public float changeMargin = 0.2f;
public bool lightison = false;
private float targetIntensity;
void Awake(){
light.enabled = false;
}
void OnTriggerEnter() {
lightison = true;
}
void OnTriggerExit() {
lightison = false;
}
void Update() {
if (lightison == true) {
light.enabled = true;
light.intensity = Mathf.Lerp (lowIntensity, highIntensity, fadeSpeed * Time.deltaTime);
} else {
light.intensity = Mathf.Lerp (highIntensity, lowIntensity, fadeSpeed *Time.deltaTime);
}
}
}
Answer by Umresh · Mar 07, 2016 at 12:04 PM
Its because of the Lerp you used the intensity of the light is not reached the destination intensity(low/final). Intensity of light keeps varying. Clamp the value using condition to avoid flickering.
Answer by tmartinez88 · Mar 09, 2016 at 06:59 AM
So @Umresh you're saying that as long as I'm onTriggerEnter then it's consistently going between low and high or vice versa and that's why it's flickering? will look at clamp haha thanks
Hey all so if you want to fade a light in or out based on a collider trigger event HERE is the script haha. This is for everybody who thought they couldnt succeeed!! ;|) enjoy!!!! $$anonymous$$ake sure your collider has "is trigger" is checked yuppp
using UnityEngine;
using System.Collections;
public class fadelight : $$anonymous$$onoBehaviour {
public Light light;
public float fadeSpeed = 2f;
public float highIntensity = 6f;
public float lowIntensity = 0.0f;
public float change$$anonymous$$argin = 0.2f;
private bool lightison = false;
private float targetIntensity;
void Awake(){
light.intensity = 0f;
targetIntensity = highIntensity;
}
void OnTriggerEnter() {
lightison = true;
}
void OnTriggerExit() {
lightison = false;
}
void Update() {
if (lightison) {
light.intensity = $$anonymous$$athf.Lerp (light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
CheckTargetIntensity ();
} else {
light.intensity = $$anonymous$$athf.Lerp (light.intensity, 0f, fadeSpeed * Time.deltaTime);
}
}
void CheckTargetIntensity() {
if($$anonymous$$athf.Abs (targetIntensity - light.intensity) < change$$anonymous$$argin)
{
if(targetIntensity == highIntensity){
targetIntensity = lowIntensity;
}
else {
targetIntensity = highIntensity;
}
}
}
}
Your answer
Follow this Question
Related Questions
Help! My Temporary Character is an Accidental Yogi! 0 Answers
Changing light color between players when they collide (BOLT) 0 Answers
Player boundary restriction 1 Answer
Can't do copy of destroyed bullet 1 Answer
OnTriggerExit does not work 0 Answers