# How to animate Timer Pocket Clock&#39;s second &amp; minute hands

Hi Guys,

I’m following a book, Unity 3.x Game Development by Example Beginner’s Guide by Ryan Henson Creighton.

I managed to create a digital timer, and a time bar & round timer clock using OnGUI 2D textures. I’m trying to animate a pocket clock timer; the minute & second hands will move accordingly with the time limit. But I’m stuck.

The issues I’m having is that
I only only animate with one hand (sec). E.g even 80sec or 20sec it will reflect as 60sec in the texture itself

1. How do I animate the minute & second hand of my pocket clock?

var centerPoint: Vector2 = Vector2(pieClockX + pieClockHalfW, pieClockY + pieClockHalfH);
var startMatrix : Matrix4x4 = GUI.matrix;
function OnGUI()
{
var minutes :int;
var seconds : int;
var timeStr : String;
minutes = timeRemaining/60;
seconds = timeRemaining % 60;
percent = timeRemaining/startTime * 100;

``````         var secRot : float = (percent/100) * 360; // *** THIS ONE

GUI.DrawTexture(clockRect2, pocketclock, ScaleMode.StretchToFill, true, 0);

GUIUtility.RotateAroundPivot(-secRot, centerPoint2);
GUI.DrawTexture(clockRect2, longstick, ScaleMode.StretchToFill, true, 0);
GUI.matrix = startMatrix; // if this is off, the whoel thing is rotating
``````

}

I tried other methods, but I end up having 70sec going from 50th (backward) moving clock wise)
And once past 60th/0 it went for a jump of 10sec

Below is the jump 10sec coding:

``````percentSec = seconds/startTime * 100;
var secRot : float = (percentSec/100) * 360; // **** THIS ONE
``````

2)Why is the sudden jump of 10sec?

I tried to break the logic into smaller parts, but it’s still very confusing to me. I couldn’t make sense out of it. If possible, can explain to me how do I break into smaller parts.
Thanks!

Well, see, you’re needlessly confusing yourself by adding ‘percent’ in, when clocks don’t really have a ‘percent.’

Your ‘seconds’ is really ‘secondsLeft’ - so when seconds is 15, the second hand points right; 30, the second hand should point straight down, and when it’s sixty or zero, it should point straight up.

It looks like RotateAroundPivot takes degrees (always check this, some Unity functions use Radians and it seems to be a bit random which uses what).

So with those two statements, we need a conversion such that (15 seconds = 90 degrees), (30 seconds = 180 degrees). Multiply by six!

GUIUtility.RotateAroundPivot( seconds * -6, centerPoint2 );

This is what I use to animate my clocks.

``````#pragma strict

var pointerSeconds : Transform;
var pointerMinutes : Transform;
var pointerHours : Transform;

private var rotationHours : int;
private var rotationMinutes : int;
private var rotationSeconds : int;
private var dt = Date();

private var clockSpeed : int = 1.0;

function Awake(){

}

function Start(){

}

function Update(){

var day = dt.Now.Day;
var month = dt.Now.Month;
var year = dt.Now.Year;
var hours = dt.Now.Hour;
var minutes = dt.Now.Minute;
var seconds = dt.Now.Second;

// Calculate pointer angles
rotationSeconds = (360.0 / 60.0)  * seconds;
rotationMinutes = (360.0 / 60.0)  * minutes;
rotationHours = ((360.0 / 12.0) * hours) + ((360.0 / (60.0 * 12.0)) * minutes);

pointerSeconds.transform.localEulerAngles = new Vector3(0, rotationSeconds, 0);
pointerMinutes.transform.localEulerAngles = new Vector3(0, rotationMinutes, 0);
pointerHours.transform.localEulerAngles = new Vector3(0, rotationHours, 0);

}
``````