Search Unity

How do I use Time.unscaledTime?

Discussion in 'Scripting' started by graviton, Oct 1, 2015.

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    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
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I am guessing you just use it as a replacement for what ever you dont want time scale to affect.

    Example
    Code (CSharp):
    1. public class Clock : MonoBehaviour
    2. {
    3.     public float currentTime;
    4.     public float currentRealTime;
    5.  
    6.     void Update()
    7.     {
    8.         currentTime = Time.time; //If TimeScale is 0, this will freeze at what ever time it was before the TimeScale was 0. Its like time has paused.
    9.  
    10.         currentRealTime = Time.unscaledTime; //If TimeScale is 0, this will still be counting up. This can be useful for a clock timer that you don't want to stop when the player pauses the game. (that is, if you use timescale to cause a pause effect)
    11.     }
    12. }

    For moving your cubes...
    Code (CSharp):
    1. public class MoveCube : MonoBehaviour
    2. {
    3.     public Transform timeCube;
    4.     public Transform unscaledTimeCube;
    5.  
    6.     void Update()
    7.     {
    8.         timeCube.Translate(Vector3.forward * Time.deltaTime); //Will freeze if timescale is 0
    9.  
    10.         unscaledTimeCube.Translate(Vector3.forward * Time.unscaledDeltaTime); //will always move
    11.     }
    12. }
     
  3. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    HiddenMonk

    Thanks for replying, I probably should have mentioned that my cubes are falling using gravity, so I'm not sure how I would use the float example or the multiplying by Time.unscaledDeltaTime example

    I thought there would be a way of getting and setting an object or function to Time.timescale independent

    kinda like how you can set a Trigger to ignore certain tags
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If both cubes use physics, then they will both use the standard timescale. There is no way to set different timescales for different objects.

    If you use your own fake physics in script, then you have more options. Depending on the complexity of physics interactions needed, this may or may not be feasible.

    An oversimplified concept of the sort of thing you would have to do, let's just make a cube that is affected by gravity, but on its own time scale (no collisions, etc, only gravity):
    Code (csharp):
    1.  
    2. public class FakeTimeScaledRigidbody : MonoBehaviour{
    3. public Vector3 velocity = Vector3.zero;
    4. public float myTimeScale = 1f;
    5. void Update() {
    6. float myDeltaTime = Time.unscaledDeltaTime * myTimeScale;
    7. transform.position += velocity * myDeltaTime;
    8. velocity += Physics.gravity * myDeltaTime;
    9. }
    10. }
    11.  
    Something you can build on at least.
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If you are using rigidbodies then I dont think there is a way to make them unaffected by Time.timeScale.
    However, not all hope is lost.

    You can instead keep your Time.timeScale untouched at 1, and then create your own custom ExtTime.timeScale that you would use instead.

    Example..
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Rigidbody))]
    4. public class Gravity : MonoBehaviour
    5. {
    6.     public float gravity = 9;
    7.     public bool affectedByTimeScale;
    8.  
    9.     Rigidbody myRigidbody;
    10.  
    11.     void Awake()
    12.     {
    13.         ExtTime.timeScale = .1f;
    14.         myRigidbody = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         Vector3 gravityForce = Vector3.down * gravity;
    20.         if(affectedByTimeScale)
    21.         {
    22.             myRigidbody.AddForce(gravityForce * ExtTime.timeScale);
    23.         }else{
    24.             myRigidbody.AddForce(gravityForce);
    25.         }
    26.     }
    27. }
    28.  
    29. public class ExtTime
    30. {
    31.     public static float timeScale = 1;
    32. }

    Now you can choose if you want it to be affected or not.
     
    Last edited: Oct 1, 2015
  6. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Well, thanks for the help guys

    It's more complicated than I thought, and not worth the trouble it'd take to implement on a larger scale, since it's so limited

    it's not all that useful