- Home /
vector2.movetowards suddenly a lot faster
I'm kind of stumped as to why this just happened but I noticed today in my game that a lot of elements that do any translation motion are now moving probably 2x-3x faster than they used to a couple days ago. I haven't updated Unity and I don't remember changing any settings that would mess with that. Time still passes by at a normal rate (I don't mess with time scale changing at all) so I'm really stumped why all my hard-coded movement speeds seem to have suddenly become so faster.
By hard-coded I mean I just stuck in numbers that seemed right for speed instead of using variables. A lot of elements of my menus and other gameplay bits use 2d translation with Vector2.movetowards, but none of it effects gameplay, so it's not harming playability. It just looks a bit silly. I could just go through and change the numbers but I'm more curious why the heck everything suddenly just changed on me!
Answer by rutter · Jun 05, 2014 at 07:39 AM
One common pitfall:
Vector2.MoveTowards(pos1, pos2, 1);
This moves up to 1 unit per frame. Which is fine... until your frame rate changes!
Instead of expressing movement in units-per-frame, you can express it in units-per-second:
Vector2.MoveTowards(pos1, pos2, 10 * Time.deltaTime);
This moves up to 10 units per second. Time.deltaTime is the number of seconds since the last frame (usually a very small number). Multiplying your movement speed by the frame time gets you a constant movement rate per second, not per frame.
Bear in mind, deltaTime
is very small (say, 0.03
), so passing in small values may yield unintuitive results.
Interesting... I haven't been using Time.deltaTime in those, but I also hadn't noticed any variability in the resulting rate of motion so I thought it was fine. I guess that's because Vsync was working correctly! (it's back to normal now so I guess somehow Vsync stopped properly limiting framerate back there for some reason...) I thought I was done making those sort of newbie mistakes, but I guess not. Thanks!
Answer by JasonBricco · Jun 05, 2014 at 07:38 AM
You probably have frame rate dependent code, and the frame rate has changed. Consider multiplying your values by Time.deltaTime when it comes to movement.
To be more detailed on that: if you're saying move x units every single frame, then if your frame rate doubles, you're moving x units twice as fast as before, so you'll see a big speed up. Multiplying by Time.deltaTime makes it based on time instead of based on frame, so it will not matter where you run it.
Edit: fixed wrong wording.
I guess it was Vsync that was keeping it consistent, then. Strange that it suddenly decided to stop (and started again today, now that it's back to normal without making any changes) but I'll definitely be changing that issue!