这个Time.deltaTime依赖transform.scale脚本如何工作?

时间:2018-02-12 16:23:03

标签: c# unity3d

我正在研究Unity3D和C#,我正在努力理解这段代码的内部工作原理。有人可以向我解释一下吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This script will shrink and grow the game object over time.
/// </summary>
public class Resizer : MonoBehaviour
{
    // fields for timer support
    const float TotalResizeSeconds = 1;
    float elapsedResizeSeconds = 0;

    // fields for resizing control
    const float ScaleFactorPerSecond = 1;
    int scaleFactorSignMultiplier = 1;

    void Update()
    {
        // resize the game object
        Vector3 newScale = transform.localScale;

        newScale.x += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;
        newScale.y += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;
        transform.localScale = newScale;

        print(Time.deltaTime);

        // update timer and check if it's done
        elapsedResizeSeconds += Time.deltaTime;
        if (elapsedResizeSeconds >= TotalResizeSeconds)
        {

            // reset timer and start resizing the game object
            // in the opposite direction
            elapsedResizeSeconds = 0;
            scaleFactorSignMultiplier *= -1;
        }
    }
}

具体来说,我试图理解这一部分:

        newScale.x += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

每次&#34;计时器&#34; scaleFactorSignMultiplier将在1-1之间切换。到达了。当累计时间超过TotalResizeSeconds时,scaleFactorSignMultiplier的符号将通过乘以-1来改变,这将控制增长和缩小之间的比例方向。

ScaleFactorPerSecond是一个常量,用于描述对象增长和缩小的速度。每隔一秒,该值将从对象的比例中添加/删除。

Time.Delta是一个系统变量,它告诉函数自上一帧计算以来经过了多长时间。这是确定在此特定帧中应用多少比例所必需的。