Created
May 9, 2025 14:04
-
-
Save theXYZT/6126979f98e391d2e5b06179e17e6099 to your computer and use it in GitHub Desktop.
Pan/Zoom Camera2D Script (Godot 4.4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Camera2D | |
const MIN_ZOOM: float = 1.0 | |
const MAX_ZOOM: float = 16.0 | |
const PAN_SPEED: float = 2000 | |
const ZOOM_SPEED: float = 1.1 | |
const ZOOM_INTERVAL: float = 0.1 | |
var zoom_tween: Tween | |
var zoom_level: float = 1.0: | |
set(value): | |
zoom_level = clamp(value, MIN_ZOOM, MAX_ZOOM) | |
if zoom_tween: | |
zoom_tween.kill() | |
zoom_tween = create_tween() | |
zoom_tween.tween_property(self, "zoom", Vector2.ONE / zoom_level, ZOOM_INTERVAL) | |
func _ready() -> void: | |
zoom_level = 3.0 | |
func _process(delta: float) -> void: | |
var input_direction := Input.get_vector("left", "right", "up", "down") | |
position += input_direction * delta * PAN_SPEED * zoom_level | |
func _unhandled_input(event: InputEvent) -> void: | |
if event is InputEventMouseButton and event.pressed: | |
match event.button_index: | |
MOUSE_BUTTON_WHEEL_UP: | |
zoom_level /= ZOOM_SPEED | |
MOUSE_BUTTON_WHEEL_DOWN: | |
zoom_level *= ZOOM_SPEED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment