animation does not change when continuously pressed the key and changed the mouse position

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

Hello Godot community. I am looking for a help to make my animation play correct animation at correct timing. I have made two different animation for the player movement. One is “walk-forward” animation, which would be played when the player is moving toward the mouse position. Second is “walk-backward” animation, which would be played when the player is moving away from the mouse position.

Each animation will be correctly played when right and left key is separately pressed. However, when you hold the key while changing the mouse position, the animation would not change. What is the cause of this problem? I would appreciate if you could teach me. Thanks.

Here is my code:

func player_run_animation() -> void:
	if is_on_floor():
		if Input.is_action_pressed("move_right") and position2D.scale.x==1: #When the right key is pressed and mouse position is at right side of the player
			_animation_player.play("Player-Run IK") #Play "walk-forward" animation
		elif Input.is_action_pressed("move_right") and position2D.scale.x==-1: # When the right key is pressed, but mouse position is opposite direction of the key input
			_animation_player.play("Player-Run IK Backward") #Play "walk-backward" animation
		elif Input.is_action_pressed("move_left") and position2D.scale.x==1: #When the left key is pressed , but mouse position is at opposite direction of the key input
			_animation_player.play("Player-Run IK Backward") #Play "walk-backward" animation
		elif Input.is_action_pressed("move_left") and position2D.scale.x==-1: ##When the left key is pressed and the mouse position is at left side of the player
			_animation_player.play("Player-Run IK") #Play "walk-forward" animation
:bust_in_silhouette: Reply From: adoci

assuming this is being called in func _process(delta): currently, it seems like you are playing the animation every frame when you hold down the key. instead, you should be calling play when the animation player isn’t already playing the same animation otherwise it will keep restarting the animation every frame.

this can be solved by doing something like the following

if Input.is_action_pressed("move_right") and position2D.scale.x==1:
    if  _animation_player.current_animation != "move_right":
        _animation_player.play("Player-Run IK")

we do this extra if statement to make sure we are not already playing the animation. and instead, wait for it to either finish or play a new animation.

Thank you for the help adocl. but this didnt solve the problem.

problem still remain:
When only the mouse position is changed from right to left while holding right key button.
the animation “walk-forward” does not change to “walk-backward”.

Oh and forgot to mention that I flip the player sprite with these script.

func get_input_direction() -> float:
	var _horizontal_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	
	if get_global_mouse_position().x > $Position2D/PlayerSkinIK.global_position.x:
		position2D.scale.x=1
	else:
		position2D.scale.x=-1
	
	return _horizontal_input 

amaou310 | 2022-07-25 07:30

Never mind. Solved the problem. But anyway thank you for your help. appreciate it.

if is_on_floor() and position2D.scale.x==1 and Input.is_action_pressed("move_right"):
	_animation_player.play("Player-Run IK")
if is_on_floor() and position2D.scale.x==-1 and Input.is_action_pressed("move_right"):
	_animation_player.play("Player-Run IK Backward")
if is_on_floor() and position2D.scale.x==-1 and Input.is_action_pressed("move_left"):
	_animation_player.play("Player-Run IK")
if is_on_floor() and position2D.scale.x==1 and Input.is_action_pressed("move_left"):
	_animation_player.play("Player-Run IK Backward")

amaou310 | 2022-07-25 08:54