Trouble with Delta Time

Godot Version

4.4

Question

I’m making a PvZ style tower defense game, and for said game I have a tower that shoots a bullet every so often.

I’ve recently discovered Engine.set_max_fps() and decided to test out slower frame rates to see how things interact in said frame rates.

Something important to know is that basically everything that moves in the project is just an Area2D, rather than a CharacterBody2D, which means I don’t have access to move_and_slide(), which already has Delta Time applied from what I hear.

This means to move something I do something basically similar to position.x += moveSpeed * moveDir

Big surprise, on slower frame rates, things move slower.

I decided to try fixing the bullets to conform to Delta Time as they are the simplest object in the game currently, so I do position.x += moveSpeed * moveDir * delta, where moveSpeed is how far an object moves in pixels, moveDir is either -1 or 1 for left and right movement, and delta comes from the _process() function.

This properly works in making the bullets move the same speed across all frame rates, however, now they are moving way too slow. After many attempts at trial and error, I’ve come up short in terms of solutions. I’ve tried rearranging the order of operations, adding and removing parentheses, and much more, but 1 of 3 things happened with each solution I’ve tried:
The bullets instantly move to the center of the screen
The bullet speed is still super slow
The bullet speed is slower/faster based on the frame rate, as if Delta Time was never applied at all.

I’ve tried position.x += (movespeed * delta * 100) * moveDir but I’m not entirely sure how good or long-term of a solution that will end up being.

I’m a bit stuck at this point. Are there any possible solutions I might’ve missed? I’m still somewhat early enough into the project where replacing every moving object with a CharacterBody2D isn’t a completely insane idea, however I am considering it a last-resort solution to this problem.

There is nothing wrong with what you are doing, you just need to set the appropriate movespeed. Multiplying it by 100 is fine, or you could just use higher values to begin with.

I’m not entirely sure what your issue is…

The main issue in this specific case is how fast the bullets move

Without delta time, I have them move at 20 pixels per frame

Using delta time, with a max framerate of 60 fps, that ends up at about 0.33 repeating pixels per frame

Honestly I could use larger values but I would like to know if there’s any solutions that might be better

Yes, when you use delta, movespeed will be pixels per second rather than per frame. So if yo still want 20 pixels per frame at 60 frames per second movespeed would have to be 1200.

3 Likes

Honestly this perfectly explains to me the issue and what delta time actuallyis, thank you so much

2 Likes