FTME01_LightRandomAndFade.cs 974 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FTME01_LightRandomAndFade : MonoBehaviour {
  4. Light plight;
  5. public float minIntensity = 0;
  6. public float maxIntensity = 2;
  7. public float flickerSpeed = 0.03f;
  8. public float fadeStartTiming = 1;
  9. public float fadeTime = 1;
  10. private float endTime = 0;
  11. private float startTime = 0;
  12. private float currentTime = 0;
  13. private bool switchOn = false;
  14. private float finalIntensity = 0;
  15. IEnumerator Start () {
  16. plight = GetComponent<Light>();
  17. while (currentTime < fadeStartTiming) {
  18. plight.intensity = (Random.Range (minIntensity, maxIntensity));
  19. yield return new WaitForSeconds(flickerSpeed);
  20. }
  21. startTime = Time.time;
  22. endTime= startTime + fadeTime;
  23. finalIntensity = plight.intensity;
  24. switchOn = true;
  25. }
  26. void Update () {
  27. currentTime += Time.deltaTime;
  28. if (switchOn == true) {
  29. plight.intensity = Mathf.InverseLerp (endTime, startTime, Time.time)* finalIntensity;
  30. }
  31. }
  32. }