0 votes

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")
in Engine by (46 points)

1 Answer

0 votes

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

by (362 points)
edited by

thank you my friend
I used Animation Tree

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.