How to zoom camera smoothly while holding down a button

Godot 4.2.1

I’ve looked around but havent found an answer to this specifically
What I want is to smoothly zoom in the camera while the player is crouching to reduce their visibility
I know how to detect when the button is pressed and when its released, and i know how to change the zoom variable in my script, but what i cant figure out is how i would go about smoothly incrementing the zoom while the button is pressed, and then smoothly zoom back out when its released. Thanks for the help

You could use an AnimationPlayer node or create_tween to animate the zoom change. Use the Input system such as overriding _unhandled_input to get when buttons are pressed and/or released.

What do you have so far?

Oh the animation player is a good idea, i’ll try that
Currently i dont have anything beyond snapping the zoom to max when the button is pressed

func _input(event: InputEvent) → void:
if event.is_action_pressed(“crouch”):
zoom(true)
elif event.is_action_released(“crouch”):
zoom(false)

func zoom(state : bool):
match state:
true:
set_zoom(get_zoom() + Vector2(6, 6))
false:
set_zoom(get_zoom() - Vector2(6, 6))

do you think it would work if i made an animation with the minimum and maximum zoom values as keyframes, and then inside the match state told it to play the animation when true, and play it in reverse when false?

edit: yes it does!

extends Camera2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _input(event: InputEvent) → void:
if event.is_action_pressed(“crouch”):
zoom(true)
elif event.is_action_released(“crouch”):
zoom(false)

func zoom(state : bool):
match state:
true:
animation_player.play(“Zoom”)
false:
animation_player.play_backwards(“Zoom”)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.