Using time delta to move an object's x position?

Hi! My game works perfectly and the max FPS is 60, But after testing on a less capable machine that only gets 30FPS I noticed a huge difference in speed of the player. I discovered TimeDelta after doing a bit of research but trying to figure out how to implement it. I tried taking the original code looks like this:
“Change the X position of ufo: add 1.5”
so, I changed it to this
“Change the X position of ufo: add 1.5*TimeDelta()”

After doing that the player doesn’t move at all, but the player can still jump if I click the jump key. Any thoughts or feedback appreciated!

Did some quick testing for ya. Your code works, but the object is just moving REALLY slowly. Try changing your 1.5 value to something much higher like 100 or whatever you find works. Hope that helps!

OB

You can also use forces, they take timedelta into account for you automatically :wink:

As it stands, that 1.5 is what you want it to move per frame, but by applying TimeDelta(), it now represents 1.5 pixels per second.

You want the multiplier to be the number of pixels the ufo is to move in one second, not the number of pixels to move each frame. If it’s 1.5 pixels per frame for a game at 60 fps, then you should make the calculation 90 * TimerDelta().

TimeDelta() returns the number of seconds since the last frame update. If it’s running ar 60fps, TimeDelta() is around 0.016. So 1.5 * 0.016 is a 0.024 - less that 1/40th of a pixel per frame.

As you researched and found out, the beaurty of TimeDelta() is that machine speed is irrelevant. On slower machine the value it returns is larger. So the overall effect is still 90 pixels per second.

1 Like