Animations not working for duplicate instances of an actor

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

I’m making a frogs and flies type game and have a simple 2 frame animation for the fly’s buzzing wings and some simple code to give it a random flight pattern. Everything runs smoothly until I try to place duplications of this fly in my main scene. Only the first fly’s animation runs, but every duplicate after that is stuck on one frame. I’ve never had this problem in other projects, placing duplicates of enemies in a scene. What could be causing this? Interestingly, if I have the scene spawn instances of the fly, the animations work fine, but I can’t place them ahead of time without this problem. Here’s the code for the fly, I don’t think the problem is here, though.

`extends KinematicBody2D

var motion = Vector2.ZERO

func _ready() → void:
horizontalMovement()
verticalMovement()

func _physics_process(delta: float) → void:
motion = move_and_slide(motion, Vector2.UP)
$Sprite.flip_h = motion.x > 0
$Sprite.flip_v = motion.y < 0

func horizontalMovement():
motion.x = rand_range(-60, -30)
yield(get_tree().create_timer(rand_range(1, 5)), “timeout”)
motion.x = rand_range(60, 30)
yield(get_tree().create_timer(rand_range(1, 5)), “timeout”)
horizontalMovement()

func verticalMovement():
motion.y = rand_range(-50, -10)
yield(get_tree().create_timer(rand_range(1, 5)), “timeout”)
motion.y = rand_range(50,10)
yield(get_tree().create_timer(rand_range(1, 5)), “timeout”)
verticalMovement()`

:bust_in_silhouette: Reply From: Thasselhof

When you duplicate a node, a number gets add to it.

So if you duplicate the node “fly” , you are gonna end up having “fly2” , “fly3” , “fly4”…

Maybe you need to do it

Also try to make your animation run in a loop from the start then duplicate from that one.

Should fix it

Hello!

Thank you for trying to answer my question. I am aware of the addition of numbers to the end of names on duplicated nodes, but that shouldn’t effect whether or not their animations work. I have a different platformer project with enemy slimes and I was able to place slime, slime2, slime3, etc. in my levels, and their animations work just fine. And I have indeed made sure the animation is set to loop (otherwise, the original node wouldn’t be animated either).

No, this is a different problem, maybe a glitch in the engine, or perhaps some sync or timing issue that is confusing the animation player? The fact that the first node animates, but not its duplicates is very strange. Since posting my question I also discovered that if I have the fly spawned multiple times, instead of placing them, that the first and second fly animate but any flies spawned after that also are stuck on one frame. What the heck is going on here!?

pattyorigami | 2020-07-14 11:48

I wish you had an answer to this because I am dealing with the exact same issue myself. Did you ever find a solution?

Keneration | 2022-07-13 16:04

If anyone else has this issue, I figured it out. When I made my actor, I did so inside the level node THEN saved the branch as a scene. When I did this, the Root Node of the AnimationPlayer (under Animation Mixer) was a relative path, something like ../..

To Solve:
I went inside the .gd of the actor and changed the Root Node to the actual Root Node of the actor (ex: my CharacterBody2D Cow). Then I went to the Animation tool and re-did the Sprite2D frame track because changing the Root Node broke it.

Just started with Godot a couple days ago, so there’s probably a more elegant solution than redoing the animations, but that bad Root Node reference was the core issue for me.

1 Like