Easy solution to stop my animation from looping in physics process

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vaanaattori
func Attack():
	if Input.is_action_pressed("Primary Action"):
		_animation_player.stop()
		_animation_player.play("Charging_Attack")
	elif Input.is_action_just_released("Primary Action"):
		_animation_player.stop()
		_animation_player.play("Attack")


func _physics_process(delta):
	Attack()

I am a big noob when it comes to programming especially games. how do i stop the function from being repeated every frame when i hold down left click

:bust_in_silhouette: Reply From: I-Want-Answers

Maybe use is_action_just_pressed instead of is_action_pressed?

is_action_pressed returns true every frame when the action is held down, whereas is_action_just_pressed returns true only the first frame when the action is held down.

func Attack():
    if Input.is_action_just_pressed("Primary Action"):
        _animation_player.stop()
        _animation_player.play("Charging_Attack")
    elif Input.is_action_just_released("Primary Action"):
        _animation_player.stop()
        _animation_player.play("Attack")


func _physics_process(delta):
    Attack()

Riiight, I feel really stupid

Vaanaattori | 2023-06-15 06:39

:bust_in_silhouette: Reply From: jgodfrey

A few possibilities…

  • If you use Input.is_action_just_pressed(), that’ll only trigger in the single frame where the input is triggered. That’s different than Input.is_action_pressed(), which fires in EVERY frame where the input is triggered.
  • You could only play an animation when no other animation is playing (where appropriate). To do that, you can use the is_playing() method of the player. So, something like:

.

if !_animation_player.is_playing():
    _animation_player.play("your_animation")