Animation control through buttons

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

I have a 3D personality
Contains three animations
I want to control the animation by pressing the buttons
For example, if you press the number one button, the walking animation will work
And I want to stop all other animation in the same character

This code is an example of what I have but is very brief

func _physics_process(delta):
    AnimationPlayer.play("idle")
    if clickBtnWalk:
        AnimationPlayer.play("walk")
:bust_in_silhouette: Reply From: Surtarso

you can use the playing property

if not AnimationPlayer.playing: play

also the idle will be read 1st and always since its the top of physics

do if button: play else: idle

I usually just separete a function to control animations… probably not the best way (a state machine would be a lot better) but it works for my small project

##------------------------Animation control - start------------------------------------
func _animation_control():
	##----------CHASE COLORS [animation player]
	if PlayerData.has_suit or is_bribed or no_search_mode:
		$ChaseColors.play("patrol")
	if PlayerData.has_weed and not PlayerData.has_suit and not is_bribed and not no_search_mode:
		$ChaseColors.play("siren")
		#add a visible siren effect here
	##-----------SPRITE ANIMATION
	if motion.x < 0: #left
		$Sprite.flip_h = false
		$Sprite/tookroach.flip_h = false
		$Sprite/tookroach.position = Vector2(-8,9)
		if is_bribed:
			$Sprite.play("bribed")
		elif PlayerData.has_suit and not is_bribed and not no_search_mode:
			$Sprite.play("suit_effect")
		elif has_snorted:
			$Sprite.play("patrol_morph")
		else:
			$Sprite.play("patrol")
			
	elif motion.x > 0: #right
		$Sprite.flip_h = true
		$Sprite/tookroach.flip_h = true
		$Sprite/tookroach.position = Vector2(8,9)
		if is_bribed:
			$Sprite.play("bribed")
		elif PlayerData.has_suit and not is_bribed and not no_search_mode:
			$Sprite.play("suit_effect")
		elif has_snorted:
			$Sprite.play("patrol_morph")
		else:
			$Sprite.play("patrol")
			
	else: #idle
		if PlayerData.dropped_coke and motion.x == 0: #if cop reached coke
			$Sprite.play("snort") #snort it
		else:
			$Sprite.play("idle")
##--------------------------Animation control - end--------------------------------

as you see the idle animation is the very last else/else in the function

thank you my friend
I used Animation Tree

mustafamax | 2020-11-07 17:03