BUBADEV

WeatherSystem

Aug 27th, 2025 (edited)
58
0
21 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WeatherSystem : MonoBehaviour
  6. {
  7.     public class CloudData
  8.     {
  9.         public Transform transform;
  10.         public float speed;
  11.         public Vector3 destinationPoint;
  12.         public bool IsMoving;
  13.     }
  14.  
  15.     [Header("Cloud Settings")]
  16.     [SerializeField] private float probabilitySpawnCloud = 0.01f;
  17.     [SerializeField] private GameObject[] cloudPrefabs;
  18.     [SerializeField] private Transform[] pointSpawnCloud_Left;
  19.     [SerializeField] private Transform[] pointSpawnCloud_Right;
  20.     [SerializeField] private Transform[] pointArrivalLeft;
  21.     [SerializeField] private Transform[] pointArrivalRight;
  22.     [SerializeField] private bool activeSpawnCloud;
  23.  
  24.     [Header("Rain Settings")]
  25.     [SerializeField] private float probabilityRain = 0.01f;
  26.     [SerializeField] private ParticleSystem rainParticle;
  27.     [SerializeField] private ParticleSystem RainRipple;
  28.     [SerializeField] private float DurationRain;
  29.  
  30.  
  31.     private List<CloudData> activeClouds = new List<CloudData>();
  32.  
  33.     public cycleDayNight cycleDayNight;
  34.     private void Start()
  35.     {
  36.         StartCoroutine(ActiveWheater());
  37.     }
  38.     void Update()
  39.     {
  40.         SpawnCloud();
  41.         MoveClouds();
  42.     }
  43.  
  44.     //cloud System//
  45.  
  46.     //Move clouds in destination position
  47.     private void MoveClouds()
  48.     {
  49.         List<CloudData> toRemove = new List<CloudData>();
  50.  
  51.         foreach (CloudData cloud in activeClouds)
  52.         {
  53.             if (!cloud.IsMoving) continue;
  54.  
  55.             cloud.transform.position = Vector3.MoveTowards(
  56.                 cloud.transform.position,
  57.                 cloud.destinationPoint,
  58.                 cloud.speed * Time.deltaTime
  59.             );
  60.  
  61.             if (Vector3.Distance(cloud.transform.position, cloud.destinationPoint) < 0.1f)
  62.             {
  63.                 cloud.IsMoving = false;
  64.                 cloud.transform.gameObject.SetActive(false);
  65.                 toRemove.Add(cloud);
  66.             }
  67.         }
  68.  
  69.         foreach (CloudData cloud in toRemove)
  70.         {
  71.             activeClouds.Remove(cloud);
  72.         }
  73.     }
  74.  
  75.     //spawm cloud in different position with probability and spawn in diffent position left or right
  76.     private void SpawnCloud()
  77.     {
  78.         if (activeSpawnCloud)
  79.         {
  80.             for (int i = 0; i < cloudPrefabs.Length; i++)
  81.             {
  82.                 if (!cloudPrefabs[i].activeInHierarchy)
  83.                 {
  84.                     int spawnIndex = Random.Range(0, cloudPrefabs.Length);
  85.                     int direction = Random.Range(0, 2);
  86.  
  87.                     cloudPrefabs[spawnIndex].SetActive(true);
  88.  
  89.                     Vector3 startPos, destPos;
  90.  
  91.                     if (direction == 0)
  92.                     {
  93.                         startPos = pointSpawnCloud_Left[Random.Range(0, pointSpawnCloud_Left.Length)].position;
  94.                         destPos = pointArrivalLeft[Random.Range(0, pointArrivalLeft.Length)].position;
  95.                     }
  96.                     else
  97.                     {
  98.                         startPos = pointSpawnCloud_Right[Random.Range(0, pointSpawnCloud_Right.Length)].position;
  99.                         destPos = pointArrivalRight[Random.Range(0, pointArrivalRight.Length)].position;
  100.                     }
  101.  
  102.                     cloudPrefabs[spawnIndex].transform.position = startPos;
  103.  
  104.                     CloudData cloudData = new CloudData
  105.                     {
  106.                         transform = cloudPrefabs[spawnIndex].transform,
  107.                         speed = Random.Range(0.5f, 1f),
  108.                         destinationPoint = destPos,
  109.                         IsMoving = true
  110.                     };
  111.  
  112.                     activeClouds.Add(cloudData);
  113.  
  114.                     Debug.Log("Cloud Spawned: " + cloudPrefabs[spawnIndex].name);
  115.                     activeSpawnCloud = false;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.    
  121.  
  122.     IEnumerator ActiveWheater()
  123.     {
  124.         while (true)
  125.         {
  126.             float day = cycleDayNight.CurrentDay;
  127.             if (day == 1)
  128.             {
  129.                 if(Random.value < probabilitySpawnCloud)
  130.                 {
  131.                     activeSpawnCloud = true;
  132.                     Debug.Log("Active Cloud Spawn");
  133.                     day = 0;
  134.                 }
  135.                
  136.                 if(Random.value < probabilityRain)
  137.                 {
  138.                     rainParticle.Play();
  139.                     RainRipple.Play();
  140.                     Debug.Log("Active rain");
  141.                     DurationRain = Random.Range(30,80);
  142.                     yield return new WaitForSeconds(DurationRain);
  143.                     day = 0;
  144.                 }
  145.             }
  146.  
  147.         }
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment