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 Unity Connect
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);
}
}
}