How to play a animation once player clicks on object?

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

I have a animation that should be played as soon as the player clicks on a object.

I have all the inputs (EG. if event is InputMouseEventButton…etc) ready, it’s the animation I want to put in, under that.

What’s the best method of doing this, or if there is another way of doing this?

:bust_in_silhouette: Reply From: rubendw03

If you work with normal sprites, you can attach a AnimationPlayer node to it, in which you create a new animation and set its loop to false. Here is some more info on animating sprites. Give the animation a name eg “move”.

When your animation is set up, add some code to the object, something like this should work:

func _input(event):
       if event is InputEventMouseButton:
            $AnimationPlayer.play("move")

The same should work for 3D, correct? (Forgot to clarify I’m working with 3D.)

What about using the same code above but getting a animation from another scene?

Also, from using another scene, how and what should I be doing in order to bring that into the script?

I’ve tried doing get_tree but wasn’t sure how to go about the process.

DeafSavage | 2020-05-04 15:01

:bust_in_silhouette: Reply From: jasperbrooks79

I visual script, you drag the animationplayer node in, then you get a list, there’s a method called ’ Play (string) ', you expand the ’ Default arguments ', and put the name, in the box, then it plays, IF button pressed_on_object = TRUE . . .

You can set it up in visual script very fast, I think it’s similar lines of code, to the box ones, maybe a bit different . . .

Animationplayer.play(string name), don’t know . . . <3