48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using System.Collections;
|
|||
|
|
|
|||
|
|
public class LightFlicker : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public float MaxReduction;
|
|||
|
|
public float MaxIncrease;
|
|||
|
|
public float RateDamping;
|
|||
|
|
public float Strength;
|
|||
|
|
public bool StopFlickering;
|
|||
|
|
|
|||
|
|
private Light _lightSource;
|
|||
|
|
private float _baseIntensity;
|
|||
|
|
|
|||
|
|
private float lastUpdateTime = 0f;
|
|||
|
|
public void Reset()
|
|||
|
|
{
|
|||
|
|
MaxReduction = 0.2f;
|
|||
|
|
MaxIncrease = 0.2f;
|
|||
|
|
RateDamping = 0.1f;
|
|||
|
|
Strength = 300;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Start()
|
|||
|
|
{
|
|||
|
|
_lightSource = GetComponent<Light>();
|
|||
|
|
if (_lightSource == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("Flicker script must have a Light Component on the same GameObject.");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
_baseIntensity = _lightSource.intensity;
|
|||
|
|
lastUpdateTime = Time.time;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//doesnt need to run every frame. every fixed frame is good enough.
|
|||
|
|
void FixedUpdate()
|
|||
|
|
{
|
|||
|
|
if (_lightSource.enabled)
|
|||
|
|
{
|
|||
|
|
if (!StopFlickering && lastUpdateTime + RateDamping < Time.time)
|
|||
|
|
{
|
|||
|
|
_lightSource.intensity = Mathf.Lerp(_lightSource.intensity, Random.Range(_baseIntensity - MaxReduction, _baseIntensity + MaxIncrease), Strength * Time.deltaTime);
|
|||
|
|
lastUpdateTime = Time.time;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|