Help on transitioning between two cameras

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Currently trying to work on a dynamic camera system for a 2D platformer

I have it currently setup where the camera naturally follows the player, and once the player enters a specific area, the camera would then change to one zoomed out and stationary.

Im currently trying to smoothly transition between these two cameras.

My current setup is that there is a transitioning camera that copies the position and zoom of the current camera before interpolating itself towards the newer camera, changing to the target camera once it reaches a specific threshold.

This attempt still appears to cause some issues involving stuttering and jutting.

If there is any other way I could approach creating a smooth transition between two cameras id love to know

gif of problem

Heres a gif of the current problem with the described approach.
When the camera is zooming out its supposed to be moving towards the position of the player camera. Instead it stays still.

Here’s the string of code that is meant to handle its position


	if Cam == $Player/Camera2D:
		if $CamTrigs/TransCamera.global_position.distance_to(Cam.get_target_position()) < 1 and\
		$CamTrigs/TransCamera.zoom.distance_to(Cam.zoom) < 0.9:
			Cam.make_current()
			camTrans = false
		
		else:
			$CamTrigs/TransCamera.global_position = $CamTrigs/TransCamera.global_position.lerp(Cam.get_target_position(), trans_speed * delta)
			$CamTrigs/TransCamera.zoom = $CamTrigs/TransCamera.zoom.lerp(Cam.zoom, trans_speed * delta)

something like this could be caused by 1 = is a too large of a value if you’re dealing with floating points.

Ok ive fixed the issue, Basically what I’ve done is that when transitioning back to the camera that follows the player, instead of using a transitional camera I instead change the zoom and global_position of the camera attached to the player and make that the current camera that interpolates to the desired position and zoom.

Reason why it wasnt working with the transitional camera is that the camera’s target position does not update if it is not the currently selected camera.

For anyone else having issue with cameras, heres my code for the functions handling camera changes.

Basically when a player enters a trigger zone, it goes through the NewCam function.

If the bool CamTrans is true, then itll go through the other function every frame processed

Though it is a little messy its how I am now handling this.

func transCamera(delta):
	if Cam == $Player/Camera2D:
		if Cam.position.distance_to(pCamPos) < 1 and\
		Cam.zoom.distance_to(pCamZoom) < 0.9:
			$Player/Camera2D.stationary = false
			Cam.position = pCamPos
			Cam.zoom = pCamZoom
			Cam.reset_smoothing()
			camTrans = false
			$Player/Camera2D.drag_horizontal_enabled = DragArray[0]
			$Player/Camera2D.drag_vertical_enabled = DragArray[1]
		
		else:
			Cam.position = Cam.position.lerp(pCamPos, trans_speed * delta)
			Cam.zoom = Cam.zoom.lerp(pCamZoom, trans_speed * delta)
			Cam.reset_smoothing()
			print(Cam.position.distance_to(pCamPos))
	else:
		if $CamTrigs/TransCamera.global_position.distance_to(Cam.global_position) < 0.1 and\
		 $CamTrigs/TransCamera.zoom.distance_to(Cam.zoom) < 0.9:
			Cam.make_current()
			camTrans = false
		
		else:
			$CamTrigs/TransCamera.global_position = $CamTrigs/TransCamera.global_position.lerp(Cam.global_position, trans_speed * delta)
			$CamTrigs/TransCamera.zoom = $CamTrigs/TransCamera.zoom.lerp(Cam.zoom, trans_speed * delta)
	
	
func NewCam(Camera, exit = true):
	await camTrans == false
	if !exit:
		
		$CamTrigs/TransCamera.position =  Cam.get_target_position()
		$CamTrigs/TransCamera.zoom = Cam.zoom
		camTrans = true
		Cam = Camera
		$CamTrigs/TransCamera.make_current()
		$CamTrigs/TransCamera.reset_smoothing()
		Cam.reset_smoothing()
		
	else:
		
		camTrans = true
		$Player/Camera2D.zoom = Cam.zoom
		$Player/Camera2D.global_position = Cam.get_target_position()
		$Player/Camera2D.stationary = true
		
		$Player/Camera2D.drag_horizontal_enabled = false
		$Player/Camera2D.drag_vertical_enabled = false
		
		Cam = $Player/Camera2D
		Cam.make_current()
		Cam.reset_smoothing()