Skip to main content
Inspiring
July 22, 2015

Time.deltaTime * Vector3 = (0,0 ,0) ???

I'm stumped by some odd behavior in Unity (5). When I attempt to adjust a Vector3 by multiplying by Time.deltaTime, it is causing my Vector3 to result in (0.0, 0.0, 0.0).


I can multiply 2 floats:
>> speed: 0.7787994, delta: 0.02, speed * delta: 0.01557599

But, when I multiple the deltaTime * a Vector3, it ALWAYS sets my Vector3 to 0,0,0:

Here is the code:

			
Debug.Log ("Vector3 before: " + movement);
movement = movement * Time.deltaTime;
Debug.Log ("Vector3 after: " + movement);


And some output:


Vector3 before: (0.5, 0.0, 0.9)
Vector3 before: (0.5, 0.0, 0.8)
Vector3 before: (0.6, 0.0, 0.8)
Vector3 after: (0.0, 0.0, 0.0) (x 100s of these)


Casting Time.deltaTime to a float did nothing. If I replace Time.deltaTime with a hardcoded float, the Vector multiplication works. This is driving my bonkers. Has anyone dealt with this problem before?

My debugging reports Time.deltaTime as type "System.Single", but I take it this is just the c# representation of a float?

Many thanks to anyone who can help! :)
    This topic has been closed for replies.

    9 replies

    Inspiring
    July 22, 2015
    I'm not seeing this on my end.

    Have you tried logging Time.deltaTime? Is it zero?
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    ridoutbAuthor
    Inspiring
    July 22, 2015
    Yes, I've tried logging Time.deltaTime. It's almost always 0.02 and you can see that it does interact well with other floats.

    Time.deltaTime is "delta" below:


    speed: 0.7787994, delta: 0.02, speed * delta: 0.01557599


    I think there is a Unity update I don't have. I'll try that and report back. Thanks for the suggestion.
    Up and Coming
    July 22, 2015
    You're not setting your Time.timeScale to 0 anywhere are you?
    Check out my Unity VR development blog: http://talesfromtherift.com @peterept
    Inspiring
    July 22, 2015
    Have you seen this?

    http://answers.unity3d.com/questions/43 ... ing-0.html
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    ridoutbAuthor
    Inspiring
    July 22, 2015
    I did read that post, and no, I'm not setting my timeScale anywhere. At least not that I'm aware of and I re-started my project from scratch to be sure.

    I upgraded to 5.1.2f1 and I'm still getting the same behavior. Is there any dependency between Unity and .Net Framework? Is there any other software I can update?

    I'll keep troubleshooting, but I'm totally stumped. I seem to be the only one with this issue. Never a good feeling. :?
    Inspiring
    July 22, 2015
    Have you tried switching which operations you do?

    For example:

    movement *= Time.deltaTime;


    movement = Time.deltaTime * movement;


    float oneOverDelta = 1 / Time.deltaTime;
    movement /= oneOverDelta;


    etc...

    Also, what if you create a totally new project with just a few lines of code. Does that work?
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    Inspiring
    July 22, 2015
    The way you're outputting the movement vector's value, it's implicitly calling movement.ToString() to convert it to a string. In that default implementation of ToString(), floating point values are formatted to only show one decimal place. 0.51 will be shown as "0.5", etc.

    So, when you're multiplying 0.5 * 0.02, you're getting a float value of 0.01, which is going to show up as "0.0" in the console. I'm guessing since you covered all the other bases here that this is your problem.

    You can do a more accurate display of vector values in the console log by doing this:

    Debug.Log(string.Format("{0}, {1}, {2}", movement.x, movement.y, movement.z));

    This will dump the full precise value of each float, and then optionally you can tweak the exact formatting by changing {0} to {0:0.000} for example, if you wanted to show three decimal places, etc.

    Hope that helps!

    And yes System.String = float.
    Titans of Space PLUS for Quest is now available on DrashVR.com
    ridoutbAuthor
    Inspiring
    July 22, 2015
    Drash, that was it! Problem solved. :D

    This possibility crossed my mind earlier, but I think I got detoured and never investigated the actual values. Thank you so much for taking the time to help me.

    Thank you to cybereality and everyone else who assisted. I know these types of problems are never what they seem. I REALLY appreciate the community support!
    Inspiring
    July 22, 2015
    A great conclusion. Thanks drash!
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV