JeCodeLeSoir

Untitled

Jul 15th, 2024 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public class Horloge : MonoBehaviour
  5. {
  6.     public event Action OnDay;
  7.     public event Action OnNight;
  8.     private Transform tx;
  9.  
  10.     public float interval = 25;
  11.     public float min = -90f;
  12.     public float max = 180f;
  13.  
  14.     public float time = 0;
  15.     public bool IsDay = false;
  16.     public bool IsCurrentDay = false;
  17.  
  18.     void Start()
  19.     {
  20.         OnDay += () =>
  21.         {
  22.             Debug.Log("Day");
  23.         };
  24.         OnNight += () =>
  25.         {
  26.             Debug.Log("Night");
  27.         };
  28.         tx = GetComponent<Transform>();
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         time += Time.deltaTime * 360f / interval;
  34.         time %= 360f;
  35.  
  36.         IsDay = min < time && time < max;
  37.  
  38.        
  39.         tx.localRotation = Quaternion.Euler(0, 0, -time);
  40.  
  41.         if (IsCurrentDay != IsDay)
  42.         {
  43.             IsCurrentDay = IsDay;
  44.             OnDayNightChange();
  45.         }
  46.     }
  47.  
  48.     void OnDayNightChange()
  49.     {
  50.         if (IsDay)
  51.         {
  52.             OnDay?.Invoke();
  53.         }
  54.         else
  55.         {
  56.             OnNight?.Invoke();
  57.         }
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment