---
title: "[Unity] 第49讲 关于时间函数的一些介绍"
author: "gqlu"
date: "2013-07-31T21:06:46Z"
tags: []
categories: ["移动开发"]
slug: "unity-49"
draft: false
lastmod: "2013-07-31T21:17:24Z"
id: 1916736
url: "https://gamedev.iteye.com/blog/1916736"
---

这里作者提到对于Time这个类的一些使用情况。

我们可以通过调节Time.timeScale来控制游戏的快慢。但这个是有前提的，只有当我们的控制逻辑中含有Time.deltaTime的时候才会有放慢或者加快的效果。如果有个GameObject的移动是不受Time.deltaTime的控制，比如像这样的 newPosition.x += speed; 而不是这样 newPosition.x += Time.deltaTime * speed; 那么这个物体就不会放慢，也可以这么理解：Update()还是按照原来的速度来调用，但Time.deltaTime变慢了。

在时间变慢了之后，Physics的效果有时候看起来不是很好，有一卡一顿的感觉，**一种**可以通过设置interpolation；**另外一种**方法更加被作者推荐，那就是提高fixed time的时间精度，即Time.fixedDeltaTime （实际上这个值可以在project settings -> Physics里面来设置）

官方的文档里面也这么说道：


> If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the same amount.
> 
> Time.fixedDeltaTime = 0.02 * Time.timeScale;
> 


timeScale变小，意味着速度变慢，而fixedDeltaTime也变小，却表示FixedUpdate()的调用次数增加了，这样精度更高了。这也隐含着FixedUpdate()的调用是收到timeScale控制的。


> Note that the fixedDeltaTime interval is with respect to the in-game time affected by timeScale.
> 


此外，作者还用到了Mathf.Lerp()这个函数。这个是表示线性插值的计算。

StartCoroutine()之前有可能还需要StopAllCoroutines()。

把声音也调慢可以使用AudioSource.pitch这个值。