1. 程式人生 > >【Unity3D自學記錄】Unity3D之自制小鐘表

【Unity3D自學記錄】Unity3D之自制小鐘表

new 一個 unity cond 代碼 enter 歐拉角 onu text

今天來寫一個小鐘表,事實上非常easy,就運用到了歐拉角。

首先創建時鐘、分鐘、秒鐘以及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度。

寫得比較簡單啊。

大家不要見笑。

效果圖:

技術分享

【Unity3D自學記錄】Unity3D之自制小鐘表