Unity 2D countdown timer (Clock ) script September 17, 2016 / , , ,

clock

  1. Create an object hierarchy;
  2. Create a script and attach it to an object;
  3. Access namespaces;
  4. Use the Update method;
  5. Rotate stuff based on time.

hierarchy

Add script on clock:

script

And clockAnimator script content:

public Transform hours;
private const float hoursToDegrees = 360f / 60f;
float timeLeft = 45.0f;

// Use this for initialization
void Start () {

}
// Update is called once per frame
void Update () {
//TimeSpan timespan = DateTime.Now.TimeOfDay;
timeLeft -= Time.deltaTime;;
//TimeSpan timespan = DateTime.Now.TimeOfDay;
if (timeLeft >= 0.0f)
hours.localRotation = Quaternion.Euler (0f, 0f,(timeLeft* -hoursToDegrees));

}