0 votes

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!

in Engine by (18 points)

1 Answer

+1 vote

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)
by (5,206 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.