Time.timeScale‘ is a property in Unity that affects the scale at which time passes in the game. This property can be used to create effects such as slow motion or to speed up the gameplay. It essentially modifies the speed at which the game’s time flows, relative to real-world time.

Here is an example to demonstrates how this property works. In this Unity scene, there is a sphere that contains a ‘Rigidbody’ component. The sphere will fall freely from initial altitude 10 when the game is running.

First, observe that ‘Time.timeScale’ default to 1.0.

long previous = 0;
private void Awake()
{
    previous = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
}

private void FixedUpdate()
{
    long timestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
    Debug.Log($"{timestamp - previous},{this.transform.position.y}");
}

Output:

98,10
344,9.996076
377,9.988228
384,9.976456
405,9.96076
425,9.94114
445,9.917596
465,9.890128
485,9.858736
504,9.82342
529,9.78418
544,9.741015
564,9.693928
585,9.642916
605,9.587979
625,9.529119
644,9.466335
664,9.399628
685,9.328996
705,9.254439
731,9.17596
745,9.093555
764,9.007228
784,8.916976
804,8.8228
824,8.7247
844,8.622676
865,8.516727
884,8.406856
905,8.293059
927,8.17534
946,8.053696
964,7.928128
985,7.798636
1004,7.66522
1024,7.52788
1045,7.386616
1065,7.241428
1084,7.092316
1105,6.93928
1125,6.78232
1145,6.621436
1165,6.456628
1185,6.287896
1207,6.11524
1224,5.93866
1244,5.758156
1265,5.573729
1285,5.385376
1304,5.1931
1325,4.996901
1345,4.796777
1365,4.592729
1385,4.384757
1405,4.172861
1425,3.957041
1445,3.737297
1464,3.513628
1484,3.286036
1505,3.05452
1525,2.81908
1546,2.579716
1564,2.336428
1585,2.089216
1605,1.83808
1625,1.58302
1645,1.324036
1665,1.061128
1685,0.7942955
1704,0.5235394
1725,0.2488593
1745,-0.02974486
1765,-0.312273
1785,-0.5987252
1804,-0.8891014
1825,-1.183402
1844,-1.481626
1864,-1.783774
1884,-2.089846
1905,-2.399842
1925,-2.713763
1945,-3.031607
1965,-3.353375
1985,-3.679067
2004,-4.008683
2025,-4.342223
2045,-4.679688
2065,-5.021076
2085,-5.366388
2104,-5.715624
2125,-6.068784
2145,-6.425868
2165,-6.786876
2185,-7.151808
2205,-7.520664
2225,-7.893444
2244,-8.270148
2265,-8.650776
2285,-9.035328
2304,-9.423804

Set ‘Time.timeScale’ default to 10.0.

long previous = 0;
private void Awake()
{
    Time.timeScale = 2.0f;
    previous = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
}

private void FixedUpdate()
{
    long timestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
    Debug.Log($"{timestamp - previous},{this.transform.position.y}");
}

Output:

101,10
385,9.996076
386,9.988228
403,9.976456
411,9.96076
416,9.94114
425,9.917596
435,9.890128
446,9.858736
455,9.82342
466,9.78418
475,9.741015
486,9.693928
496,9.642916
506,9.587979
516,9.529119
526,9.466335
536,9.399628
547,9.328996
556,9.254439
578,9.17596
579,9.093555
586,9.007228
596,8.916976
606,8.8228
618,8.7247
626,8.622676
635,8.516727
646,8.406856
656,8.293059
665,8.17534
676,8.053696
686,7.928128
695,7.798636
706,7.66522
716,7.52788
726,7.386616
736,7.241428
746,7.092316
755,6.93928
770,6.78232
776,6.621436
786,6.456628
796,6.287896
806,6.11524
816,5.93866
825,5.758156
835,5.573729
845,5.385376
856,5.1931
866,4.996901
876,4.796777
886,4.592729
896,4.384757
905,4.172861
916,3.957041
926,3.737297
936,3.513628
946,3.286036
956,3.05452
971,2.81908
976,2.579716
985,2.336428
996,2.089216
1006,1.83808
1016,1.58302
1026,1.324036
1036,1.061128
1045,0.7942955
1059,0.5235394
1066,0.2488593
1076,-0.02974486
1085,-0.312273
1096,-0.5987252
1106,-0.8891014
1116,-1.183402
1126,-1.481626
1136,-1.783774
1145,-2.089846
1156,-2.399842
1166,-2.713763
1176,-3.031607
1186,-3.353375
1196,-3.679067
1206,-4.008683
1216,-4.342223
1226,-4.679688
1236,-5.021076
1246,-5.366388
1256,-5.715624
1265,-6.068784
1279,-6.425868
1286,-6.786876
1296,-7.151808
1305,-7.520664
1315,-7.893444
1326,-8.270148
1336,-8.650776
1345,-9.035328
1356,-9.423804

We can observe that when ‘Time.timeScale’ is set to 1, it takes approximately 2301 millisecond for the y value to update 100 times. However, it takes only 1305 ms when ‘Time.timeScale’ is set to 2. Despite this, the magnitude of change in y’s position remains the same. In simpler term, the real-time speed can increase up to twice the normal rate.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注