Set Time Scale#
Time / Set Time Scale
Description#
Sets the coefficient value at which time passes
Note
The time scale is global and affects all game objects
Note
A value of 1 is normal speed. Lower values make the game run in slow motion
Note
A value of 0 pauses the game
Note
The transition runs in real time so the ramp keeps progressing while time is slowed or paused
Parameters#
| Name | Description |
|---|---|
| Time Scale | The new time scale value |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Title("Set Time Scale")]
[Image(typeof(IconClock), ColorTheme.Type.Blue, typeof(BadgeHourglass))]
[Description("Sets the coefficient value at which time passes")]
[Category("Time/Set Time Scale")]
[Parameter("Time Scale", "The new time scale value")]
[Note("The time scale is global and affects all game objects")]
[Note("A value of 1 is normal speed. Lower values make the game run in slow motion")]
[Note("A value of 0 pauses the game")]
[Note("The transition runs in real time so the ramp keeps progressing while time is slowed or paused")]
[Keywords("Slow", "Motion", "Fast", "Speed", "Bullet", "Pause", "Freeze", "Scale")]
[Serializable]
public class InstructionCoreTimeSetTimeScale : Instruction
{
private const string TWEEN_KEY = "time-scale";
[SerializeField] private GetNumber m_TimeScale = new GetNumber(1);
[SerializeField] private TweenTransition m_Transition = new TweenTransition(
0f,
EaseMode.InOut,
TimeMode.RealTime,
true
);
[NonSerialized] private bool m_IsComplete;
[NonSerialized] private Action<bool, float> m_CallOnUpdate;
[NonSerialized] private Action m_CallOnFinish;
[NonSerialized] private Func<bool> m_CallIsComplete;
protected override string GetTitle(GameObject source)
{
string scale = TextUtils.ThemeAsKeyword("Time Scale");
return $"Set {scale} = {this.m_TimeScale}{this.m_Transition}";
}
protected override async Awaitable Run(Args args)
{
bool wait = this.m_Transition.Await;
this.m_IsComplete = false;
this.m_CallOnUpdate ??= this.OnUpdate;
this.m_CallOnFinish ??= this.OnFinish;
this.m_CallIsComplete ??= this.IsComplete;
TweenManager.Run(new TweenStaticInput<float>(
TWEEN_KEY,
Time.timeScale,
Mathf.Max(0f, this.m_TimeScale.GetFloat(args)),
this.m_CallOnUpdate,
wait ? this.m_CallOnFinish : null,
this.m_Transition.GetDuration(args),
this.m_Transition.EaseMode,
TimeMode.RealTime
));
if (wait) await this.Until(this.m_CallIsComplete);
}
private void OnUpdate(bool isAlive, float value)
{
if (isAlive) Time.timeScale = Mathf.Max(0f, value);
}
private void OnFinish() => this.m_IsComplete = true;
private bool IsComplete() => this.m_IsComplete;
}
}