Hello, I am attempting to create a analog clock in unity to learn basic coding. I have used example code from the unity manual/documentation and from a website, combining these to make a clock.
I am trying to instantiate spheres along the outer radius of the clock to create “minute pointers” but these become elliptic instead of circular
My code is
using UnityEngine;
using System.Collections;
using System;
public class ClockAnimator : MonoBehaviour {
public Transform hours, minutes, seconds;
public GameObject prefab;
public int numberOfObjects = 360;
public float radius = 5f;
private const float
hoursToDegrees = 360f / 12f,
minutesToDegrees = 360f / 60f,
secondsToDegrees = 360f / 60f;
// Use this for initialization
void Start () {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle) * radius, 0);
Instantiate(prefab, pos, Quaternion.identity);
}
}
// Update is called once per frame
void Update () {
TimeSpan timespan = DateTime.Now.TimeOfDay;
hours.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalHours * -hoursToDegrees);
minutes.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalMinutes * -minutesToDegrees);
seconds.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalSeconds * -secondsToDegrees);
}
}