How to limit camera zoom based on a tilemap size?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wdylanbibb

I have a project that generates a map based on a tilemap size and I want the player to be able to zoom in and out. The only problem is that if the player zooms out too much, they will surpass the bounds of the tilemap. I was wondering if there was a way to clamp the camera’s maximum zoom. Thanks!

:bust_in_silhouette: Reply From: avencherus

Sure, you can use clamp() and write your own zoom method, or do the clamps where you would otherwise be calling zoom changes.

extends Camera2D

const MIN_ZOOM = 0.5
const MAX_ZOOM = 1.5

func clamped_zoom(p_zoom : float) -> void:
	
	p_zoom = clamp(p_zoom, MIN_ZOOM, MAX_ZOOM)
	zoom = Vector2(p_zoom, p_zoom)

func _ready():
	
	clamped_zoom(2.0)
	print(zoom)

	clamped_zoom(0.1)
	print(zoom)