Godot Logo

Fixing Physics with Time Scaling in Godot

Slowing down your games speed can make key moments of gameplay so much more interesting. However, you can’t always just set Engine.time_scale without potentially having a few issues with your physics. You may notice that when you slow down time, rigid bodies tend to keep much more momentum. To fix this, you should scale your physics_ticks_per_second with your time_scale.

# Store the default physics ticks for reset
var base_physics_ticks: int = Engine.physics_ticks_per_second

func set_time_scale(target_time_scale: float) -> void:	
	# Scale physics_ticks by time_scale
	var target_iterations = int(base_physics_ticks * time_scale) 

	Engine.time_scale = target_time_scale
	Engine.physics_ticks_per_second = target_iterations


func reset_time_scale() -> void:
	# Reset to defaults
	Engine.time_scale = 1.0
	Engine.physics_ticks_per_second = base_physics_ticks

I would recommend adding these functions to an autoload so you can call these functions anywhere with Global.set_time_scale(float) and Global.reset_time_scale() and easily use Godot time scaling to improve the feel of your game.

Similar Posts