How to play an animation only when a key is pressed

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

I am implementing ladders in my 2D platformer.

The user presses up or down when the Player node intersects a Ladder node to enter a climb state. This also snaps the Player to the Ladder and disables gravity. If the user releases the up or down button, the Player stays put on the Ladder.

When in the climb state, I only want to play my climb animation when the user is holding up or down. If they release the button, I want the animation to freeze.

How can I achieve this in Godot 3.0?

I am controlling my animations with an AnimationPlayer.

:bust_in_silhouette: Reply From: Socrates

You might want to create a state machine for you character and show animatiions based on the character’s state, but the most direct way is just:

if Input.is_action_pressed(“name_of_action”):
$“name of animation”)


if Input.is_action_just_released("name_of_action"):

The false parameter tells the AnimationPlayer not to reset the animation.

Thanks for the answer! I do use a state machine. I will take your suggestion and whenever my Player is in the climb state, I will check whether input is pressed or not. Depending on the input, I will play or pause the animation.

Diet Estus | 2018-04-03 04:44