Created
August 25, 2018 23:15
-
-
Save Auskennfuchs/2c3986247bdcae72c6478497434225b1 to your computer and use it in GitHub Desktop.
Unity Weather System Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| public class WeatherSystem : MonoBehaviour | |
| { | |
| [Serializable] | |
| public class Season | |
| { | |
| public float duration; | |
| public Color lightColor; | |
| public float intensity; | |
| public ParticleSystem particles; | |
| } | |
| [Header("Weather Settings")] | |
| public Season[] seasons; | |
| public int currentYear = 0; | |
| public Light sunLight; | |
| public Season currentSeason; | |
| private int currentSeasonIndex = 0; | |
| private float seasonTime; | |
| void Start() { | |
| currentSeason = seasons[currentSeasonIndex]; | |
| seasonTime = currentSeason.duration; | |
| } | |
| void Update() { | |
| seasonTime -= Time.deltaTime; | |
| if (seasonTime <= 0.0f) { | |
| currentSeasonIndex++; | |
| if (currentSeasonIndex >= seasons.Length) { | |
| currentSeasonIndex = 0; | |
| currentYear++; | |
| } | |
| if(currentSeason.particles!=null) { | |
| currentSeason.particles.Stop(); | |
| } | |
| currentSeason = seasons[currentSeasonIndex]; | |
| seasonTime = currentSeason.duration; | |
| if (currentSeason.particles != null) { | |
| currentSeason.particles.Play(); | |
| } | |
| } | |
| LerpLight(sunLight, currentSeason.lightColor,currentSeason.intensity); | |
| } | |
| private void LerpLight(Light light, Color c, float intensity) { | |
| float t = Time.deltaTime * 0.2f; | |
| light.color = Color.Lerp(light.color, c, t); | |
| light.intensity = Mathf.Lerp(light.intensity, intensity, t); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment