Unity三维基础-模拟时钟加深对Quaternion理解

一个简单的时钟演示

1:画好时间表盘,时,分,秒 指针, 注意要三个指针统一父级空物体的坐标点。然后调用指针大小位置材质对好~  后面的方法是以Y轴旋转的,最好以地面俯视角度画物体。演示的 ObjClock里除了三指针外其它层物体都是跑龙套的,参考可忽略~

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbXl0aGxvbmcuY29t,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbXl0aGxvbmcuY29t,size_20,color_FFFFFF,t_70,g_se,x_16 

2:给父级拖上写好的代码 ,把对应的三指针拖上去,就可~  (没啥技术含量,只供新人学习)

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbXl0aGxvbmcuY29t,size_15,color_FFFFFF,t_70,g_se,x_16

3:运行效果。

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbXl0aGxvbmcuY29t,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAbXl0aGxvbmcuY29t,size_20,color_FFFFFF,t_70,g_se,x_16 

4:码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// author:mythlong
/// 这是一个模拟时钟表盘旋转的方法 
/// </summary>
public class MythlClock : MonoBehaviour
{
    public Transform hourObj;
    public Transform minuteObj;
    public Transform secondObj;

    private float hour;
    private float minute;
    private float second;
    private DateTime nowTime;
    // Start is called before the first frame update
    void Start()
    {
        nowTime = DateTime.Now;
        hour = nowTime.Hour;
        minute = nowTime.Minute;
        second = nowTime.Second; 
        secondObj.rotation = Quaternion.AngleAxis(second * 6, Vector3.up);  
    }

    // Update is called once per frame
    void Update()
    {
        nowTime = DateTime.Now;
        hour = nowTime.Hour;
        minute = nowTime.Minute;
        second = nowTime.Second;

        //Debug.Log("time:" + hour + " : " + minute + " : " + second);
        Quaternion tmpSec = Quaternion.AngleAxis(second * 6, Vector3.up);
        //secondObj.rotation = Quaternion.Lerp(secondObj.rotation, tmpSec, Time.deltaTime);
        secondObj.rotation = Quaternion.RotateTowards(secondObj.rotation, tmpSec, Time.deltaTime*6);

        minuteObj.rotation = Quaternion.AngleAxis(minute*6+ second * 6/60, Vector3.up);
        hourObj.rotation = Quaternion.AngleAxis(hour*30+minute*6/12, Vector3.up);         
    }
}

猜你喜欢

转载自blog.csdn.net/mythl/article/details/120397829
今日推荐