- Home /
How do I use Time.unscaledTime?
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
$$anonymous$$ake certain your input detection is within an Update and not a FixedUpdate As the later is bound by timeScale and will not listen to your set of commands.
Answer by landon912 · Oct 02, 2015 at 12:17 AM
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.
Answer by Suddoha · Oct 02, 2015 at 02:04 AM
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);
}
}
Answer by outasync · Oct 02, 2015 at 02:02 AM
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.
Your answer
Follow this Question
Related Questions
Is it possible to change Time.timeScale to a value bigger than 100? 0 Answers
Time hasn't stopped 1 Answer
Trouble with Time.Timescale 2 Answers
Framerate Independent Firing Rate (delta time?) 2 Answers
Time.realTimeSinceStartup behaves weirdly when sending app to background or locking the screen. 1 Answer