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
I am guessing you just use it as a replacement for what ever you dont want time scale to affect. Example Spoiler: Click for clock example Code (CSharp): public class Clock : MonoBehaviour { public float currentTime; public float currentRealTime; void Update() { 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. 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) } } For moving your cubes... Spoiler: Click for moving cube example Code (CSharp): public class MoveCube : MonoBehaviour { public Transform timeCube; public Transform unscaledTimeCube; void Update() { timeCube.Translate(Vector3.forward * Time.deltaTime); //Will freeze if timescale is 0 unscaledTimeCube.Translate(Vector3.forward * Time.unscaledDeltaTime); //will always move } }
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
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): public class FakeTimeScaledRigidbody : MonoBehaviour{ public Vector3 velocity = Vector3.zero; public float myTimeScale = 1f; void Update() { float myDeltaTime = Time.unscaledDeltaTime * myTimeScale; transform.position += velocity * myDeltaTime; velocity += Physics.gravity * myDeltaTime; } } Something you can build on at least.
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.. Spoiler: Click for code Code (CSharp): using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class Gravity : MonoBehaviour { public float gravity = 9; public bool affectedByTimeScale; Rigidbody myRigidbody; void Awake() { ExtTime.timeScale = .1f; myRigidbody = GetComponent<Rigidbody>(); } void FixedUpdate() { Vector3 gravityForce = Vector3.down * gravity; if(affectedByTimeScale) { myRigidbody.AddForce(gravityForce * ExtTime.timeScale); }else{ myRigidbody.AddForce(gravityForce); } } } public class ExtTime { public static float timeScale = 1; } Now you can choose if you want it to be affected or not.
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