Holding and reseting animation on different key press

Godot Version

Godot Engine v4.5.1

Question

I started working with Godot three days ago and im kinda doing things on my own with help from youtube tutorials and, well this forum but i cant seem to find something that would help me with my problem.

Im making small 2D game and for now im implementing movment and animations and i want to make my attack/block act different on press and hold. On press it plays animation which i already have in animation player and on hold i want to keep last frame of animation and cant quite figure it out. Animation always plays fully and stops at last frame.

I have it set up like this and dont really know what to do next.

@onready var combat = $AnimationPlayer

func _combat(_delta: float) → void:
if Input.is_action_just_pressed(“Hit”):
combat.play(“Strike”)
elif Input.is_action_pressed(“Hit”):

if Input.is_action_just_pressed(“Block”):
combat.play(“Defend”)
elif Input.is_action_pressed(“Block”):

1 Like

You can try using the AnimationFinished signal to check if you are still holding the input, then you would need to make a “defend_idle” animation to switch to which would just be your final frame.

Then you have to make a system to switch to your default animation when you release the input.

If you were using AnimatedSprite2D you can control the frame you are on with FrameChanged signal, and the frame property. It would be the same input checking logic but you wouldn’t need to create a new animation, you would just set frame = last_frame

“On press it plays animation which i already have in animation player and on hold i want to keep last frame of animation and cant quite figure it out.”

“Animation always plays fully and stops at last frame”

Is the second sentence a “rewording” of what you want to happen? Or is it a problem? Because it sounds like what you want to happen is already happening…

Thanks for answering, i will try to put it clearly.
Im using animation player for those attack/block animation. My goal is to make attack and block keep that last frame where they are extend from player and after realase i want them to go back to idle state and similar to just a single click, they extend and return. In this moment they extend and stay there and after clicking again they snap back to starting position and play animation again/

Okay, thank you for clarifying! You can add the function if Input.is_action_just_released() to swap back to your default animation.