Why is my deltaTime wrapper slightly faster than time.deltaTime?
Hey folks.
I have a singleton GameController static script accessed by a Get property called Instance which contains a public float called dTime. In the Update method, dTime is assigned Time.deltaTime, with the intention of later multiplying this by a scalar value and creating an independent time scale (for bullettime, etc).
To test that this was working, I left out the scalar value and I used it to move the rigidbody component attached to my player character's motor script, like so:
rbMotor.MovePosition(rbMotor.position + desiredDirection * speedCur * GameController.Instance.dTime
Weirdly, when compared to this:
rbMotor.MovePosition(rbMotor.position + desiredDirection * speedCur * Time.deltaTime);
... the first piece of code (the wrapped Time.deltaTime) seems to move my character slightly, but noticeably, FASTER than the normal Time.deltaTime.
Further tests show that this effect happens with other game elements when I use dTime over Time.deltaTime.
(This is how I assign the dTime float in my GameController static class singleton...)
dTime = Time.deltaTime ;
I've used this technique in the past - it's usually very reliable. This time, however, it's being weird.
Has anybody idea what could be causing this? How can I fix this problem?
--Rev
EDIT #02: Okay, what the absolute fuck. Stuck a debug statement just above where the MovePosition code is called...
Debug.Log("dTime is " + GameController.Instance.dTime + " and Time.deltaTime is " + Time.deltaTime);
... resulting in the following output in the console:
dTime is 0.3333333 and Time.deltaTime is 0.02
UnityEngine.Debug:Log(Object)
CharMotor:MoveCharRelative() (at Assets/Scripts/Motors/CharMotor.cs:160)
PlayInput:FixedUpdate() (at Assets/Scripts/Player/PlayInput.cs:51)
What the chittering hellfuck? dTime is OBVIOUSLY larger than Time.deltaTime, though they should be equivalent.
...
Nnngh. Could this be due to calling MovePosition in FixedUpdate, sampling dTime at a fixed period instead of getting a fresh Time.deltaTime when called to move... and thereby getting an arbitrarily high figure with dTime to multiply everything by?
Is this what I get for trying to call my rigidbody move position and rotate functions in fixed update?
I think I'll try creating a fixed update delta time, see if that works.
EDIT #03 - How weird. Well, I did as I said above, created a fixed update delta time called fTime (assigning Time.deltaTime in FixedUpdate()) and used this whenever I called a rigidbody MovePosition / MoveRotation command and... it seems to be working as a workable equivalent to Time.deltaTime. Caveat emptor, however - I'm testing this on a very crappy laptop, and it's possible I'm not seeing glitchy behavior above the machine's normal performance.
I think this might be the solution to this problem - in which case I'll write an answer for this in the near future. However, if anybody has any input or advice, would appreciate hearing it.
Answer by Reverend-Speed · May 29, 2017 at 11:59 AM
Solution seems to be as above (Edit 02/03) - create a second wrapper in your singleton, this time for Time.deltaTime sampled from FixedUpdate(). Use this when moving objects by Rigidbody Move functions and bob's yer uncle.
Your answer
Follow this Question
Related Questions
Farming game Time management 0 Answers
Is Time.deltaTime different on various devices? 0 Answers
Load scene after time 1 Answer
How to use time, when timeScale is 0? 2 Answers
Returning abnormal values with Time.deltaTime and Time.unscaledDeltaTime. 0 Answers