Create a Real Time Analog Wall Clock using C#
Script an analog real time wall clock with C#. Rotate and animate the hands using the awesome iTween tool.
❤️ Subscribe to Oxmond Tutorials:
https://bit.ly/SubscribeOxmondTutorials
✅ Download free Analog Wall Clock assets here:
https://www.freepik.com/free-vector/designer-wall-clock-in-a-metal-casing_1310847.htm
Designed by Iconicbestiary
✅ Download the free iTween tool here:
https://assetstore.unity.com/packages/tools/animation/itween-84?aid=1100l4p9k
✅ Other free Asset Packages at the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k
😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE
Clock script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Clock : MonoBehaviour { public GameObject secondHand; public GameObject minuteHand; public GameObject hourHand; string oldSeconds; void Update () { string seconds = System.DateTime.UtcNow.ToString("ss"); if (seconds != oldSeconds) { UpdateTimer(); } oldSeconds = seconds; } void UpdateTimer() { int secondsInt = int.Parse(System.DateTime.UtcNow.ToString("ss")); int minutesInt = int.Parse(System.DateTime.UtcNow.ToString("mm")); int hoursInt = int.Parse(System.DateTime.UtcNow.ToLocalTime().ToString("hh")); print(hoursInt + " : " + minutesInt + " : " + secondsInt); iTween.RotateTo(secondHand, iTween.Hash("z", secondsInt * 6 * -1, "time", 1, "easetype", "easeOutQuint")); iTween.RotateTo(minuteHand, iTween.Hash("z", minutesInt * 6 * -1, "time", 1, "easetype", "easeOutElastic")); float hourDistance = (float)(minutesInt) / 60f; iTween.RotateTo(hourHand, iTween.Hash("z", (hoursInt + hourDistance) * 360 / 12 * -1, "time", 1, "easetype", "easeOutQuint")); } }
Does itween only exist in Unity 2020 or is it elsewhere? I am working on a clock in unity 2019 and can’t find it in the package manager.
You can find download iTween for free in the asset store:
https://assetstore.unity.com/packages/tools/animation/itween-84
Good morning! I have a question for you Hans. I designed a clock using your script in this tutorial but for some reason when the second hand reaches twelve things go backwards as if it’s being reset to zero instead of just continuing on it’s way. It’s weird. I was wondering if it’s unity or the script that is causing this effect. Thank you in advance for any advice you might have on this matter. You’re tutorials are wonderful.