How do I stop players from moving the character during death and victory animations in a platformer?

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

So I’m building a platformer game, for an independent study. The game’s almost done. The main issue that’s left is turning off player control during the death and victory animations. As it currently stands, I have this code set up that releases the buttons, but can easily be overrided by pressing the buttons again:

func _on_goal():
Input.action_release("left")
Input.action_release("right")

Is there a work-around that will permanently disable player control during the animations?

:bust_in_silhouette: Reply From: samjmiller

I solved this problem by calling queue_free() on the player when they die, but it depends on your set-up (if the camera is a child of the player, it will disappear too; death animations might not play if they’re children of the player, etc).

You can disable input with set_process_input(false), so one way to do it might be:

func die():  
     set_process_input(false)
     yield(get_tree().create_timer(1.0), "timeout")
     queue_free()

The timer you create with yield(get_tree().create_timer(1.0), "timeout") would give you space to play the animation (make it as long as the animation), and then you can remove the player from the scene.

Hope this helps!

I tried putting in the set_process_input(false)portion on my character for both when the character dies in the game or beats the level. It still doesn’t work. And as an added problem, because you can still move the character, you get hit by an enemy even as you’re dying. So I had to write extra code to stop the character’s health from dropping below zero. My character does have a finite state machine that controls it’s movements. Could that be interfering with the command to turn off player input?

ThatElfNerd | 2022-04-09 05:42

Not sure - definitely try playing around with that, see if disabling the finite state machine and handling motion another way makes a difference.

You can also try set_block_signals(true) instead of set_process_input(false) - might have the desired effect.

samjmiller | 2022-04-09 22:03

:bust_in_silhouette: Reply From: Shineurysm

Just check whether the player is alive or while doing an animation dance before the movement, for example:

var is_alive = true
var is_dancing = false

if is_alive == true and is_dancing == false:
    # movement....