Time.deltaTime(只读)
表示上一帧完成的时间,使用这个函数和游戏的帧速率没有关系。如果我们需要物体的移动以秒来执行.我们需要将物体移动的值乘以Time.deltaTime。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
transform.Translate(0 , 0 , 1 );
}
}
如果我们想游戏对象可以按每秒的速度去执行移动的方法我们需要这样做
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
transform.Translate(0 , 0 , 1 *Time.DeltaTime);
}
}
Time.time 时间(只读)
从游戏开始到到现在所用的时间。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject projectile;
public float fireRate = 0.5 F;
private float nextFire = 0.0 F;
void Update() {
if (Input.GetButton("Fire1" ) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
duck clone = Instantiate(projectile, transform.position, transform.rotation);
}
}
}
Time.fixedTime(只读)
FixedUpdate已经执行的时间
Time.fixedDeltaTime(可读可写)
fixedDeltatime是一个固定的时间增量。 【注】可以通过fixedDeltatime来改变FixedUpdate的跟新速率。
在Update中time、deltaTime获取的是一个准确的值,而fixedTime的值并不会增加。 在FixedUpdate中fixedTime会更新并和Time.time保持一致。deltaTime和fixedDeltatime的值保持一致