I know what it does but I’m not sure how to use it in my code
All the answers I’ve found just point to how it’s a good way to move or animate when Time.timescale == 0
I need some simple example code
I have 2 moving Cubes, I want one to keep moving when game is paused
public class ScaledMoving : MonoBehaviour
{
private void Update ()
{
//move constantly 5 units per second independent of time scale
transform.position += transform.position += Vector3.right * 5f * Time.unscaledDeltaTime;
//move 5 units at full time scale; will change depending on setting
transform.position += transform.position += Vector3.right * 5f * Time.deltaTime;
}
}
Here is a simple example of how to create a cube moving independent of the frame rate and/or time scale. Delta time is the time since the last frame.
Just like you’d use Time.deltaTime, here’s a short example:
public class Example : MonoBehaviour {
// assign these both in the inspector
public GameObject go1;
public GameObject go2;
private bool paused = false;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
paused = !paused;
Time.timeScale = paused? 0f : 1f;
}
go1.transform.Rotate(Vector3.up, 30 * Time.deltaTime);
go2.transform.Rotate(Vector3.up, 30 * Time.unscaledDeltaTime);
}
}
If you set Time.timescale to 0 then you won’t be able to rotate your cube unless you use the animator, simply create your rotating cube animation and set the animator Update Mode to Unscaled time.