Physics process function leads to fluctuating framerates

Godot Version

4.2.2

Question

I noticed a stuttery fps when I played a level in my game with lots of enemies. Their script has a pretty large _physics_process function, so I tried to see what happened if I deleted it and found that my fps became super smooth. I then profiled it and noticed my Physics Time (which seems to be linked to the _physics_process function) fluctuated between 0ms and 5ms. I then reduced the _physics_process function to only a few simple lines and it still fluctuated between 0ms and, this time, 1.5 ms. Faster, but it still felt slightly stuttery. Does anyone know why this happens and is there any way to make the _physics_process time more stable?

I would also like to mention that my physics fps is 60. This is the _physics_process function:

func _physics_process(delta):
	if target == null:
		dist_from_target = 10000
	else:
		dist_from_target = Vector2(target.position.x, target.position.z).distance_to(Vector2(position.x, position.z))
	if not is_active() or not alive:
		return

physics_time

How many enemies do you have active at a time?

If enemies are too far away, consider disabling their _physics_process() method using set_physics_process(false) when appropriate.

The physics process runs in a loop between 0 and up to 8 times a frame.

Use it for things that require a stepped update, aka the next call on that function depends on the result from the last call.

Everything else has no reason to exist inside a physics process function. People throw their entire AI, gameplay logic and visual rendering updates inside physics process and move half their node tree with it wondering why performance is so bad.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.