How to rotate a camera around a scene in 90deg increments?

Godot Version

4.2.2

Question

I have a scene, around which the player character moves and jumps in. The camera is in a fixed position above the map. I want the camera position to be able to rotate in 90 degrees increments with a smooth camera movement around that map, in a way so that all four “angles” of the scene is covered (North, West, South, East).

I have a script that enables the rotation how I like it, but if I go beyond 360 rotation (i.e. rotating the map in one direction for long enough), then the game bugs out where moving the player character causes the map to spin around.

Here’s what I have as the code:

func _process(delta):
...
	$Origin/CameraPivot.rotation_degrees = $Origin/CameraPivot.rotation_degrees.lerp(target_rotation, rotation_speed * delta)
	if Input.is_action_just_released("rotate_left"):
		target_rotation.y -= 90
	if Input.is_action_just_released("rotate_right"):
		target_rotation.y += 90

Rotation are in radians, and 90° is equivalent to π/2.0.

To smooth it out you can use a vector lerp or a transform lerp. To do that appropriately, you should set a target rotation in the input and lerp the current to the target.

Just be aware that rotation of the object are not saved as expected and will overflow to at some point most likely at 360° back to zero. But there are other axis that will do this at 90°.