- Home /
Time.deltatime multiplied by variable is different than by a number literal
After being away from unity for a while I've started a new project, and I've working on something that needs Time.deltatime, but I'm getting weird results.
Multiplying Time.deltatime by a number literal yields a different result that multiplying Time.deltatime by a variable with the same value. It's off by a factor of 1000... I can't tell if I'm overlooking something in UnityScript that's causing this to occur.
I'm using Unity ver 4.5.0f6 on Mavericks, scripting in UnityScript. Here's a stripped down example and what I'm getting as output.
#pragma strict
var fade_out_speed : float = 5.0f;
function Update () {
Debug.Log("==========");
Debug.Log(Time.deltaTime);
Debug.Log(Time.deltaTime * 5.0f);
Debug.Log(Time.deltaTime * fade_out_speed);
Debug.Log(Time.deltaTime * fade_out_speed * 1000);
}
Gives me the output
==========
UnityEngine.Debug:Log(Object)
0.01775795
UnityEngine.Debug:Log(Object)
0.08878976
UnityEngine.Debug:Log(Object)
8.878976E-05
UnityEngine.Debug:Log(Object)
0.08878976
UnityEngine.Debug:Log(Object)
interesting... after specifying it to be public it didn't affect the calculation in C#... definite difference between float and double though :)
seem like the output converted to second if datatype is float ins$$anonymous$$d of "milisecond". Could be Unity framework bug?
Try print that value to a UI element ins$$anonymous$$d of the console, avoid that Debug.Log function.
Answer by robertbu · Jun 19, 2014 at 03:40 AM
Since fade_out_speed is public, check in the Inspector to make sure the value is 5.0. Or alternately, make fade_out_speed private and rerun the test.
Wow, that was exactly it. I hadn't touched it in the inspector yet, but making it private did the trick!
Answer by tswalk · Jun 19, 2014 at 04:13 AM
perhaps it's an issue with their java precision... no idea, but when I cast it as a float in C# there's a bit of rounding happening on the operations. Casting as doubles is interesting though :)
public class dtimer : MonoBehaviour
{
private bool _executed;
void OnEnable()
{
_executed = false;
}
void Update()
{
double fade_out_speed = 5.0d;
double dtime = Time.deltaTime;
double _calculated;
string _log;
if (!_executed)
{
Debug.Log("==========");
_calculated = dtime;
_log = string.Format("deltaTime: {0:N9}", _calculated);
Debug.Log(_log);
_calculated = dtime * 5.0d;
_log = string.Format("deltaTime * 5.0d: {0:N9}", _calculated);
Debug.Log(_log);
_calculated = dtime * fade_out_speed;
_log = string.Format("deltaTime * fade_out_speed: {0:N9}", _calculated);
Debug.Log(_log);
_calculated = dtime * fade_out_speed * 1000.0d;
_log = string.Format("deltaTime * fade_out_speed * 1000.0d: {0:N9}", _calculated);
Debug.Log(_log);
_executed = true;
}
}
}
results:
deltaTime: 0.020000000
deltaTime * 5.0d: 0.099999998
deltaTime * fade_out_speed: 0.099999998
deltaTime fade_out_speed 1000.0d: 99.999997765
Your answer
Follow this Question
Related Questions
Cable-building simulations. 0 Answers
Overload Operator in UnityScript? 0 Answers
Touch.deltaTime confusion 0 Answers
Time.deltaTime help 1 Answer