AnalogClock.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. namespace TurnTheGameOn.Timer
  2. {
  3. using UnityEngine;
  4. using System;
  5. public class AnalogClock : MonoBehaviour
  6. {
  7. public bool useSystemTime = true;
  8. public Timer timer;
  9. public Transform secondHandPivot;
  10. public Transform minuteHandPivot;
  11. public Transform hourHandPivot;
  12. private float secondhandRotation;
  13. private float minuteRotation;
  14. private float hourRotation;
  15. private int second;
  16. private int minute;
  17. private int hour;
  18. void Update ()
  19. {
  20. if (useSystemTime)
  21. {
  22. second = (int)DateTime.Now.Second;
  23. minute = (int)DateTime.Now.Minute;
  24. hour = (int)DateTime.Now.Hour;
  25. }
  26. else
  27. {
  28. if (timer)
  29. {
  30. second = (int)timer.second;
  31. minute = (int)timer.minute;
  32. hour = (int)timer.hour;
  33. }
  34. else
  35. {
  36. Debug.LogWarning ("This clock is not set to use system time, please assign a timer to get values from.");
  37. }
  38. }
  39. secondhandRotation = second * 6f;
  40. minuteRotation = minute * 6f;
  41. hourRotation = (hour * 30) + (minuteRotation/12);
  42. secondHandPivot.localRotation = Quaternion.Euler(0, 0, -secondhandRotation);
  43. minuteHandPivot.localRotation = Quaternion.Euler(0, 0, -minuteRotation);
  44. hourHandPivot.localRotation = Quaternion.Euler(0, 0, -hourRotation);
  45. }
  46. }
  47. }