You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请教依赖Time.deltaTime的transform.scale脚本工作原理

Hey there! Let's break down how using Time.deltaTime with transform.scale works in Unity—this is a core concept for making consistent, frame-rate independent animations, so it's great you're diving into it.

Why Do We Use Time.deltaTime?

First, let's get clear on what Time.deltaTime actually is: it's the time (in seconds) that passed between the last frame and the current frame.

Here's the problem without it: if you modify transform.scale directly in Update() (which runs once per frame), your object's scaling speed will depend on how many frames your device can render. A high-end PC running at 60 FPS will execute your code 60 times per second, while a slower mobile device at 30 FPS will run it only 30 times. Without Time.deltaTime, your object would scale twice as fast on the PC—totally inconsistent!

Time.deltaTime fixes this by tying your scaling to real time instead of frame count, so the speed stays the same no matter the device.

A Basic Linear Scaling Example & Breakdown

Let's start with a simple, common script snippet:

void Update()
{
    // Scale the object by 1 unit on all axes every second
    transform.localScale += Vector3.one * Time.deltaTime;
}

Let's unpack each part:

  • transform.localScale: This gets (or sets) the object's scale relative to its parent (if it has one; if not, it's world scale). It's a Vector3 with x, y, and z values representing the scale on each axis.
  • Vector3.one: Shortcut for new Vector3(1, 1, 1)—it's just a quick way to apply the same change to all three axes.
  • * Time.deltaTime: Multiplying by the frame time converts our "per-frame" change into a "per-second" change. For example:
    • At 60 FPS, Time.deltaTime is ~0.0167 seconds. Each frame, we add ~0.0167 to each scale axis. Over 60 frames (1 second), that adds up to 1.
    • At 30 FPS, Time.deltaTime is ~0.0333 seconds. Each frame adds ~0.0333, and over 30 frames that also adds up to 1.

Result: The object scales by 1 unit per second, no matter the frame rate.

How It Works Under the Hood

Unity's Update() method is called once per rendered frame. When you modify transform.scale here:

  1. Without Time.deltaTime, your change is frame-based—each frame you add a fixed value, so total change per second = fixed value × FPS.
  2. With Time.deltaTime, your change is time-based—each frame you add fixed value × Time.deltaTime, so total change per second = fixed value × (FPS × Time.deltaTime). But since FPS × Time.deltaTime always equals ~1 (because Time.deltaTime = 1/FPS), your total per-second change stays constant.

This is the magic of Time.deltaTime: it normalizes your frame-based operations to real time.

Beyond Linear Scaling: Exponential Growth

Sometimes you want to scale by a percentage instead of a fixed value (e.g., double the object's size every second). For that, you'll use multiplication instead of addition, and you need to account for continuous growth:

void Update()
{
    float scaleMultiplierPerSecond = 2f; // Double size every second
    transform.localScale *= Mathf.Pow(scaleMultiplierPerSecond, Time.deltaTime);
}

Why Mathf.Pow()? Because exponential growth is continuous. If you just did transform.localScale *= 1 + Time.deltaTime, you'd end up with a growth rate of ~2.718x per second (since (1 + dt)^(1/dt) approaches Euler's number e as dt gets small). Using Mathf.Pow() lets you explicitly set the exact per-second multiplier you want.

Key Takeaways
  • Time.deltaTime makes your scaling (and any movement/animation) frame-rate independent, so it behaves consistently across all devices.
  • For linear scaling (adding a fixed amount per second), use transform.localScale += [value] * Time.deltaTime.
  • For exponential scaling (multiplying by a percentage per second), use transform.localScale *= Mathf.Pow([multiplier], Time.deltaTime).
  • Always use Time.deltaTime in Update() when you want to tie changes to real time—avoid it in FixedUpdate() though, since FixedUpdate() runs at a fixed time step (use Time.fixedDeltaTime there instead).

内容的提问来源于stack exchange,提问作者Nicola Del Monaco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:52:45