StellarXipher/Assets/Scripts/CeilingLightController.cs

62 lines
1.6 KiB
C#

using UnityEngine;
using System.Collections;
public class CeilingLightController : MonoBehaviour
{
private Light ceilingLight;
private Coroutine fadeCoroutine;
private bool isFading = true;
private Color defaultColor;
private float defaultIntensity;
public float fadeDuration = 0.5f;
void Start()
{
ceilingLight = GetComponent<Light>();
defaultColor = ceilingLight.color;
defaultIntensity = ceilingLight.intensity;
ceilingLight.color = Color.red;
ceilingLight.intensity = 0;
fadeCoroutine = StartCoroutine(FadeInOut());
}
IEnumerator FadeInOut()
{
while (isFading)
{
float timer = 0f;
while (timer < fadeDuration)
{
timer += Time.deltaTime;
float t = timer / fadeDuration;
ceilingLight.intensity = Mathf.Lerp(0, defaultIntensity, t);
yield return null;
}
ceilingLight.intensity = defaultIntensity;
timer = 0f;
while (timer < fadeDuration)
{
timer += Time.deltaTime;
float t = timer / fadeDuration;
ceilingLight.intensity = Mathf.Lerp(defaultIntensity, 0, t);
yield return null;
}
ceilingLight.intensity = 0;
}
}
public void StopFading()
{
if (fadeCoroutine != null)
{
StopCoroutine(fadeCoroutine);
fadeCoroutine = null;
}
isFading = false;
ceilingLight.color = defaultColor;
ceilingLight.intensity = defaultIntensity;
}
}