Skip to content

Instantly share code, notes, and snippets.

@nukadelic
Created September 14, 2019 15:42
Show Gist options
  • Save nukadelic/c59c09aec4f709e4f4d1b4176c455bdb to your computer and use it in GitHub Desktop.
Save nukadelic/c59c09aec4f709e4f4d1b4176c455bdb to your computer and use it in GitHub Desktop.
Test Unity time scale and physics rigidbody jitter
using UnityEngine;
// Create a plane and a cube above that plane, attach a rigidbody to the cube & add gravity
// drop this script anywhere on the scene and watch the variables in realtime
// mess around with timeScale and see the results
// toggle the jump checkbox during runtime
public class TestTimeScale : MonoBehaviour
{
[Space,Space,Range(0.01f, 3f)]
public float timeScale = 0.5f;
[Header("total time")]
public float time = 0;
public float fixedTime = 0;
public float unscaledTime = 0;
public float fixedUnscaledTime = 0;
public float timeSinceLevelLoad = 0;
[Header("delta time")]
public float deltaTime = 0;
public float fixedDeltaTime = 0;
public float fixedUnscaledDeltaTime = 0;
public float smoothDeltaTime = 0;
public float unscaledDeltaTime = 0;
[Header("physics")]
public Rigidbody target = null;
public bool jump = false;
void Start()
{
Time.timeScale = timeScale;
}
void FixedUpdate()
{
fixedTime = Time.fixedTime;
fixedDeltaTime = Time.fixedDeltaTime;
fixedUnscaledTime = Time.fixedUnscaledTime;
fixedUnscaledDeltaTime = Time.fixedUnscaledDeltaTime;
moveTarget();
}
void Update()
{
Time.timeScale = timeScale;
time = Time.time;
deltaTime = Time.deltaTime;
unscaledTime = Time.unscaledTime;
smoothDeltaTime = Time.smoothDeltaTime;
unscaledDeltaTime = Time.unscaledDeltaTime;
timeSinceLevelLoad = Time.timeSinceLevelLoad;
}
void moveTarget()
{
if( jump )
{
if( target != null )
{
Vector3 f = Time.deltaTime * Vector3.up * 300;
target.AddForce( f , ForceMode.Impulse );
}
jump = false;
}
}
}
@nukadelic
Copy link
Author

when changing timeScale the following will

change

  • deltas:
    • Time.deltaTime
    • Time.fixedUnscaledDeltaTime ( ? )
    • Time.smoothDeltaTime
  • timers:
    • Time.time
    • Time.fixedTime
    • Time.timeSinceLevelLoad

not change

  • deltas:
    • Time.fixedDeltaTime ( ? )
    • Time.unscaledDeltaTime
  • timers:
    • Time.unscaledTime
    • Time.fixedUnscaledTime

Note: fixedUnscaledTime will start breaking down when timescale will be near zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment