- Home /
Triggering candle lights on with Colliders
I have created numerous point lights that will randomly change in intensity like a flickering candle. All of these lights working at the same time however really slows down the game engine. So how can I trigger these lights using my FPSController to turn on when colliding. I have created a sphere collider for each individual point light and a sphere collider for my FPScontroller. Could someone help me with scripting this in C#
This is my script so far that allows the lights to flicker. I understand that i should use OnTriggerEnter and OnTriggerExit i just don't know how to script this all. Any help would greatly be appreciated, I'm a beginner using scripts and unity :)
using System; using UnityEngine; using Random = UnityEngine.Random;
[RequireComponent(typeof (Light))] public class TorchLight : MonoBehaviour {
public float MinLightIntensity = 0.6F;
public float MaxLightIntensity = 1.0F;
public float AccelerateTime = 0.15f;
private float _targetIntensity = 1.0f;
private float _lastIntensity = 1.0f;
private float _timePassed = 0.0f;
private Light _lt;
private const double Tolerance = 0.0001;
private void Start() {
_lt = GetComponent<Light>();
_lastIntensity = _lt.intensity;
FixedUpdate();
}
private void FixedUpdate() {
_timePassed += Time.deltaTime;
_lt.intensity = Mathf.Lerp(_lastIntensity, _targetIntensity, _timePassed/AccelerateTime);
if (Math.Abs(_lt.intensity - _targetIntensity) < Tolerance) {
_lastIntensity = _lt.intensity;
_targetIntensity = Random.Range(MinLightIntensity, MaxLightIntensity);
_timePassed = 0.0f;
}
}
}
Answer by Quertie · Apr 30, 2016 at 06:03 PM
If I am understanding this right, you want the light to flicker only if the player is within a certain area around it. Since the flickering of the light is coded in the FixedUpdate() function, you need some way to know if the player is inbound this area. All you really need is a bool ;)
using System;
using UnityEngine;
using Random = UnityEngine.Random;
[RequireComponent(typeof (Light))] public class TorchLight : MonoBehaviour {
public float MinLightIntensity = 0.6F;
public float MaxLightIntensity = 1.0F;
public float AccelerateTime = 0.15f;
private float _targetIntensity = 1.0f;
private float _lastIntensity = 1.0f;
private float _timePassed = 0.0f;
private Light _lt;
private const double Tolerance = 0.0001;
public bool inBound; // Add a bool to know if player is inbound or not
private void Start() {
_lt = GetComponent<Light>();
_lastIntensity = _lt.intensity;
FixedUpdate();
}
private void OnTriggerEnter(Collider col) // When player enters area, set inBound to true
{
if(col.gameObject.Tag == "Player")
{
inBound = true;
}
}
private void OnTriggerExit(Collider col) // When player leaves area, set inBound to false
{
if(col.gameObject.Tag == "Player")
{
inBound = false;
}
}
private void FixedUpdate() {
if(inBound) // logic only applies if player is inbound
{
_timePassed += Time.deltaTime;
_lt.intensity = Mathf.Lerp(_lastIntensity, _targetIntensity, _timePassed/AccelerateTime);
if (Math.Abs(_lt.intensity - _targetIntensity) < Tolerance) {
_lastIntensity = _lt.intensity;
_targetIntensity = Random.Range(MinLightIntensity, MaxLightIntensity);
_timePassed = 0.0f;
}
}
}
}
Make sure you have a Collider with isTrigger enabled on your light, and that your character has a "Player" tag, and this should work :)