【蓝鸥Unity开发基础三】课时11 Time和Mathf类

推荐视频讲师博客:http://11165165.blog.51cto.com/

一、工具类

Time类中封装了常用的时间控制方法

Mathf类中封装了常用的数学方法

【蓝鸥Unity开发基础三】课时11 Time和Mathf类_蓝鸥


接下来让我们在Unity演示一下首先添加一个Cube——为Cube添加一个Test脚本。

二、Time

1、Time.time属性

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {


    void Update () {
        if (Input.GetKeyDown(KeyCode.P)) {


            //Time类用来进行时间控制
            float t= Time.time;

            print ("从游戏开始到当前帧,所消耗的总时长为" + t + "");
        }

    }
} 

 


2Time.deltaTime 属性

 

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {


    void Update () {
        if (Input.GetKeyDown(KeyCode.P)) {

            //从上一帧开始到当前帧结束,这两帧之间的时间间隔
            float  d= Time.deltaTime;

            print (d);
        }

    }
} 

【蓝鸥Unity开发基础三】课时11 Time和Mathf类_蓝鸥_02

新的需求:Cube每秒钟照着Y轴旋转30度,使用Time.deltaTime来实现。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {


    void Update () {

        //让当前游戏对象每秒钟准确旋转30
        transform.Rotate (Vector3.up,Time.deltaTime*30f);

    }
} 

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    public float angLeSpeed;

    void Update () {


        //让当前游戏对象每秒钟准确旋转angLeSpeed
        transform.Rotate (Vector3.up,Time.deltaTime*angLeSpeed);

    }
} 

3、Time.timeScale 属性

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    public float angLeSpeed;

    void Update () {

        //表示时间流逝的快慢
        //1——表示正常时间流逝
        //2——表示时间流逝加快,是正常速度的两倍
        //0——表示时间停止,游戏暂停
        float ts = Time.timeScale;


    }
} 

 

三、Mathf

 

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    public float angLeSpeed;

    void Update () {

        //求绝对值
        int i=Mathf.Abs(-12);// i=12

        //求最大最小值,可变参数
        int m= Mathf.Max(12,16,44,3,6);
        int ni = Mathf.Min (3,5);

        //三角函数
        Mathf.Sin();
        Mathf.Cos ();
        Mathf.PI ;
        int s= Mathf.Sqrt (4);




    }
}