How to play animation before movement

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

So I have created a cutout walking animation. And I have coded it to play when the character moves right. The problem is the character moves before the animation plays and the animation won’t play unless the movement stops.

This is the code. (Not the full code. I can paste full code if you want but I thought that was unnecessary)

func _process(delta):
	velocity=Vector2()
	
	if Input.is_action_pressed("ui_right"):
		$Animations_Character.play("Walk")
		velocity.x +=1

Animations_Character is the Animationplayer node

Found the solution I think. It’s working.

func _process(delta):
velocity=Vector2()

if Input.is_action_pressed("ui_right"):
	if ($Animations_Character.current_animation!="Walk"):
		$Animations_Character.play("Walk")
	velocity.x +=1

Skydome | 2018-05-31 23:55

:bust_in_silhouette: Reply From: eons

The problem is that you are restarting the animation all the time while the action is pressed, you want to play it only once.

There are many ways to check that, one way can be checking if the animation playing is not the movement one, if it is not, play it.

Like:

if Animations_Character.current_animation != "Walk":
  Animations_Character.play("Walk")

The platformer demos have a more complex animation setup, separated from the motions, it stores the current animation name to decide finally if play it or not, leaving “idle” as default.