Skip to content

Instantly share code, notes, and snippets.

@lubien
Created January 13, 2024 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubien/46e5d1e43d055da13e2b3ca158839f25 to your computer and use it in GitHub Desktop.
Save lubien/46e5d1e43d055da13e2b3ca158839f25 to your computer and use it in GitHub Desktop.
godot 4.2 smooth switch between cameras
extends Node2D
var camera2D: Camera2D
var tween: Tween
func _ready():
camera2D = $Camera2D
func transition_camera2D(from: Camera2D, to: Camera2D, duration: float = 1.0) -> void:
if transitioning: return
# Copy the parameters of the first camera
camera2D.zoom = from.zoom
camera2D.offset = from.offset
camera2D.light_mask = from.light_mask
# Move our transition camera to the first camera position
camera2D.global_transform = from.global_transform
# Make our transition camera current
camera2D.make_current()
tween = create_tween()
tween.set_parallel(true)
tween.set_ease(Tween.EASE_IN_OUT)
tween.set_trans(Tween.TRANS_CUBIC)
tween.tween_property(camera2D, "global_transform", to.global_transform, duration).from(camera2D.global_transform)
tween.tween_property(camera2D, "offset", to.offset, duration).from(camera2D.offset)
tween.play()
await tween.finished
to.make_current()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment