How do I change camera zoom using a script?

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

(title)

:bust_in_silhouette: Reply From: SingingApple

All you need is here: Camera2D — Godot Engine (3.0) documentation in English
Check out the last option in the member variables.
You should be able to use that to change the zoom in script. I don’t know how the code will look like bcoz I don’t use Godot 3 .

:bust_in_silhouette: Reply From: Ulukanovich

Hello! I’m from Russia. Sorry for my English.

I use this way:

  1. Project > Project Settings > Input Map: create actions ‘wheel_up’ and ‘wheel_down’. 2) Add mouse buttons ‘Wheel Up’ and ‘Wheel Down’ for All Devices
  2. And use this script:
func zoom():
    	
    	if Input.is_action_just_released(wheel_down'):
    		$Camera2D.zoom.x += 0.25
    		$Camera2D.zoom.y += 0.25
    	if Input.is_action_just_released('wheel_up') and $Camera2D.zoom.x > 1 and $Camera2D.zoom.y > 1:
    		$Camera2D.zoom.x -= 0.25
    		$Camera2D.zoom.y -= 0.25
    	
 func _physics_process(delta):
    		
    	zoom()

hey, do you know how I would create a maximum zoom?

Amateur.game.dev. | 2021-03-06 20:56

1 Like
:bust_in_silhouette: Reply From: TheYellowArchitect

Ulukanovich’s answer doesn’t work as it is in 4.0, so I converted it for 4.0:

extends Camera2D


func zoom():
	if Input.is_action_just_released('wheel_down'):
		set_zoom(get_zoom() - Vector2(0.25, 0.25))
	if Input.is_action_just_released('wheel_up'): #and get_zoom() > Vector2.ONE:
		set_zoom(get_zoom() + Vector2(0.25, 0.25))

func _physics_process(delta):
	zoom()

The differences are:

  • I commented out the limit
  • I reversed the zoom so its intuitive (wheel up doesnt zoom out!)
  • Removed $Camera2D since I used the above script on the camera itself. You could easily place back the $Camera2D

I keep coming across people saying that X doesn’t work for Godot 4 and providing a solution just for that and I made an account just to start saying thank you. It’s helping me so much in learning the engine and starting off on 4.

TheFoxKnocks | 2023-04-29 16:40

I am happy to hear that my reply helped! Godot 4 is insanely powerful, and what prevents users from tapping into that power is not something lacking in the engine, but users cannot understand how to use something (lack of documentation)
Documentation has already heavily improved in the last few months, just like its stability (both of which are Godot’s Achilles’ heel)

I hope whatever feature you want for your game, you find good documentation, may that be text, video or even a full demo :slight_smile:

TheYellowArchitect | 2023-04-30 16:24

:bust_in_silhouette: Reply From: Cire_arievilo1

See this video:

How to zoom using the scroll wheel in Godot 3.2