【Unity3D自学记录】Unity3D之自制小钟表

今天来写一个小钟表,事实上非常easy,就运用到了欧拉角。

首先创建时钟、分钟、秒钟以及4个点(12点、3点、6点、9点)偷懒了~~没弄那么多点。

时钟、分钟、秒钟这三个父级的中心一定要注意,我们旋转的是父级的欧拉角。


(父级的中心在子级的顶点)

如图:

【Unity3D自学记录】Unity3D之自制小钟表

接下来我们来写代码。例如以下:

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度。

写得比較简单啊。

大家不要见笑。

效果图:

【Unity3D自学记录】Unity3D之自制小钟表