Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public class ColorCamera : MonoBehaviour
  2. {
  3. [SerializeField]
  4. private Color colorSky;
  5. public Color ColorSky { get { return colorSky; } set { colorSky = value; } }
  6.  
  7. [SerializeField]
  8. private Color colorSpace;
  9. public Color ColorSpace { get { return colorSpace; } set { colorSpace = value; } }
  10.  
  11. [SerializeField, Tooltip("Number of seconds to switch between sky/space background colors")]
  12. private float colorSwitchSeconds = 0.5f;
  13. public float ColorSwitchSeconds { get { return colorSwitchSeconds; } set { colorSwitchSeconds = value; } }
  14.  
  15. Transform player;
  16.  
  17. public bool PlayerIsInSpace { get { return player ? player.position.y > 10f : false; } }
  18.  
  19. void Start()
  20. {
  21. GameObject playerGO = GameObject.FindGameObjectWithTag("Player");
  22. if (playerGO)
  23. {
  24. player = playerGO.transform;
  25. StartCoroutine(SetBackgroundColor());
  26. }
  27. else
  28. {
  29. Debug.LogWarning("No player found in scene.");
  30. }
  31. }
  32.  
  33. IEnumerator SetBackgroundColor()
  34. {
  35. bool isSky = PlayerIsInSpace;
  36. float lastSwitchTime = Time.time;
  37. while (true)
  38. {
  39. Color oldColor = Camera.main.backgroundColor;
  40. Color newColor = isSky ? ColorSky : ColorSpace;
  41. float t = (Time.time - lastSwitchTime) / ColorSwitchSeconds;
  42. Camera.main.backgroundColor = Color.Lerp(oldColor, newColor, t);
  43.  
  44. yield return null;
  45. bool isNowSky = PlayerIsInSpace;
  46. if (isNowSky != isSky)
  47. lastSwitchTime = Time.time;
  48. isSky = isNowSky;
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement