蓝鸥Unity开发教程之课时11 Time和Mathf类

本文介绍了Unity中Time类和Mathf类的基本用法。Time类包括time、deltaTime和timeScale等属性的应用,Mathf类则涉及Abs、Max、Min等数学方法。通过具体示例展示了如何利用这些类实现游戏对象的精确旋转及数学运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 蓝鸥Unity开发教程之课时11 Time和Mathf类

一、工具类

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);
        }

    }
} 

 

新的需求: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);




    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值