How to have a animation order?
Asked Answered
H

2

0

I am building a 2d top down pixel rpg in godot using AnimatedSprite2D node. When I press e an attack animation happens. But if you press e while running, the attack animation gets cancelled by the walking animation. I want to make it so that whenever the e key is pressed, the player can't move anymore and other animations get cancelled other than the attacking animation.

I tried to make a timer that lasted as long as the attack animation, which is 60 milliseconds. It would activate if the e key was pressed, and it would then turn some variables that were needed for animation and movement to false. However it did not work. i Seached a tutorial where they were using a AnimationPlayer node, however it requires a sprite2D node instead of AnimatedSprite2D. Do you maybe know a function that could help me here? If this doesnt work, i will continue with the game, and when i make a new one, i will use a AnimationPlayer node

Heteronym answered 12/11, 2023 at 22:21 Comment(0)
M
0

I haven't used AnimatedSprite2D before. I don't like it very much compared to just animating a Sprite2D. The best way to handle something like this is with an AnimationTree, which the tutorial probably also was using. That said, it still can be done with AnimatedSprite2D just fine. Rather than setting a timer, try waiting for the animation to finish. Here is an example:

extends AnimatedSprite2D

var can_transition := true

func _ready() -> void:
	try_play_anim(&"default")

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_accept"):
		if await try_play_anim(&"attack", false):
			try_play_anim(&"default")

func try_play_anim(anim:StringName, can_interrupt = true) -> bool:
	if !can_transition:
		return false

	if can_interrupt:
		play(anim)
		return false

	can_transition = false
	play(anim)
	await animation_finished
	can_transition = true
	return true

The try_play_anim function is a helper function and a coroutine. When we play the attack, we don't want it interrupted, so we set the sentinel bool can_transition and then turn it back off once it is done. If we play all of our animations through this function, it will guarantee that animations that shouldn't be interrupted will not be. In your movement code, you can check on the can_transition value to see if you are allowed to move.

This is an example of a very primitive state machine with only two states: can_transition=true and can_transition=false. For more complex sets of animations, it is better to make a fully fleshed out state machine on your own or to use the one Godot provides in AnimationTree.

Modern answered 12/11, 2023 at 23:49 Comment(0)
H
0

Modern Thanks a lot, i will work on this 😃

Heteronym answered 13/11, 2023 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.