• 2D
  • How to apply an animation to a node created in code?

I'm in a situation where nodes are created dynamically and need to be animated. To achieve this, I have defined an "AnimationPlayer" as a template in the node tree. It affects the "position" property. I want to achieve two things:

  • Run one of the animations of the animation player on my dynamically created node
  • Run the animation relative to node's current position (ie if the animation moves the node 10 units to the left, it should do that starting from the current position)

What's the prescribed way of doing this in Godot? From other system I know that animation can be defined and then applied. This doesn't seem to be the case here.

Duplicate the AnimationPlayer node, (optionally) make it a child of animated node and then change paths to animated properties in animation tracks using AnimationPlayer.track_set_path().

Alternatively consider making a scene containing the animated node and an AnimationPlayer and just instance the whole scene when needed.

Depends how complex your animations are, but it may be easier to do everything in code, for example with Tween. You can set starting and ending positions. But if you need complex circular movement, then you might need to use AnimationPlayer.

Tween is exactly what I was looking for! How could I miss it? Thanks!