Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ColorCamera : MonoBehaviour
- {
- [SerializeField]
- private Color colorSky;
- public Color ColorSky { get { return colorSky; } set { colorSky = value; } }
- [SerializeField]
- private Color colorSpace;
- public Color ColorSpace { get { return colorSpace; } set { colorSpace = value; } }
- [SerializeField, Tooltip("Number of seconds to switch between sky/space background colors")]
- private float colorSwitchSeconds = 0.5f;
- public float ColorSwitchSeconds { get { return colorSwitchSeconds; } set { colorSwitchSeconds = value; } }
- Transform player;
- public bool PlayerIsInSpace { get { return player ? player.position.y > 10f : false; } }
- void Start()
- {
- GameObject playerGO = GameObject.FindGameObjectWithTag("Player");
- if (playerGO)
- {
- player = playerGO.transform;
- StartCoroutine(SetBackgroundColor());
- }
- else
- {
- Debug.LogWarning("No player found in scene.");
- }
- }
- IEnumerator SetBackgroundColor()
- {
- bool isSky = PlayerIsInSpace;
- float lastSwitchTime = Time.time;
- while (true)
- {
- Color oldColor = Camera.main.backgroundColor;
- Color newColor = isSky ? ColorSky : ColorSpace;
- float t = (Time.time - lastSwitchTime) / ColorSwitchSeconds;
- Camera.main.backgroundColor = Color.Lerp(oldColor, newColor, t);
- yield return null;
- bool isNowSky = PlayerIsInSpace;
- if (isNowSky != isSky)
- lastSwitchTime = Time.time;
- isSky = isNowSky;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement