今天来写一个小钟表,其实很简单,就运用到了欧拉角。
首先创建时钟、分钟、秒钟以及4个点(12点、3点、6点、9点)偷懒了~~没弄那么多点。
时钟、分钟、秒钟这三个父级的中心一定要注意,我们旋转的是父级的欧拉角。
(父级的中心在子级的顶点)
如图:
接下来我们来写代码,如下:
using UnityEngine;
using System.Collections;
public class Clock : MonoBehaviour {
public Transform shi;
public Transform fen;
public Transform miao;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log("时"+System.DateTime.Now.Hour);
Debug.Log("分"+System.DateTime.Now.Minute);
Debug.Log("秒"+System.DateTime.Now.Second);
//秒钟
float miaonum=System.DateTime.Now.Second*6f;
miao.eulerAngles = new Vector3(miao.position.x, miao.position.y, -miaonum);
//分钟
float fennum = System.DateTime.Now.Minute * 6f;
fen.eulerAngles = new Vector3(fen.position.x, fen.position.y, -fennum);
//小时
float shinum = System.DateTime.Now.Hour * 6f;
shi.eulerAngles = new Vector3(shi.position.x, shi.position.y, -shinum);
}
}
所有的值都要 *6 ,因为一共60秒,一圈是360度,1秒就是6度。
写得比较简单啊。
大家不要见笑。。。
效果图: