FogControl.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Camera))]
  4. [RequireComponent(typeof(UnderWaterFog))]
  5. [ExecuteInEditMode]
  6. public class FogControl : MonoBehaviour {
  7. public float FadeSpeed = 10f;
  8. private float Rate = 1f;
  9. private UnderWaterFog fog;
  10. private Camera cam;
  11. void OnEnable(){
  12. init ();
  13. }
  14. void Start(){
  15. init ();
  16. }
  17. void Update () {
  18. Rate += Time.deltaTime / FadeSpeed;
  19. Rate = Mathf.Clamp(Rate, 0, FadeSpeed);
  20. //Under Water
  21. if (cam.transform.position.y <= fog.height) {
  22. if (!fog.enabled) {
  23. fog.enabled = true;
  24. }
  25. fog.fogColor.a = Mathf.Lerp(fog.fogColor.a, 1f, Rate);
  26. } else {
  27. //Over water
  28. fog.fogColor.a = Mathf.Lerp(fog.fogColor.a, 0f, Rate * 2f);
  29. if (fog.fogColor.a <= 0.01f) {
  30. fog.enabled = false;
  31. }
  32. }
  33. }
  34. private void init(){
  35. if (cam == null) {
  36. cam = GetComponent<Camera> ();
  37. }
  38. if (fog == null) {
  39. fog = GetComponent<UnderWaterFog> ();
  40. }
  41. if (cam.transform.position.y >= fog.height) {
  42. fog.fogColor.a = 0f;
  43. }
  44. }
  45. }