Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
ridoutb's avatar
ridoutb
Protege
11 years ago

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! :)

9 Replies

Replies have been turned off for this discussion
  • I'm not seeing this on my end.

    Have you tried logging Time.deltaTime? Is it zero?
  • 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.
  • 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. :?
  • 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?
  • drash's avatar
    drash
    Heroic Explorer
    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.
  • 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!