Translating a game object's instance in Animation

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

Trying to topple enemy game object’s instance when got hit by player’s bullet.
Animation works but the toppled game object global_transform.origin changes to the last enemy game object’s instance global_transform.origin. When the animation starts to play the enemy game object instance 1 is positioned at enemy game object’s instance no.2’s position and ends the animation there.

I used only two instances of the same enemy game object. The following code is in the script of the enemy game object.

func configAnimation():
    var anim = $AnimationPlayer.get_animation("Topple")
	var p:Vector3 = self.transform.origin
	print("p   " + String(p))
	var p1:Vector3 = p
	p1.y = p1.y + 2

	print("p1   " + String(p1))

I connected the animation_finished signal to the AnimationPlayer node and got the position of the game object instance number one starting and ending the animation at the position of game object instance number 2.
The Output is like this:
p (14.8615, -4.71206, 0)
p1 (14.8615, -2.71206, 0)
p (10.2427, -4.71206, 0)
p1 (10.2427, -2.71206, 0)

Anim Finished
(10.2427, -4.71206, 0)

Any suggestion will be helpful. Thanks.

do you have a single animation node or an animation node for each enemy?

Andrea | 2021-01-20 15:34

I have one single enemy object in which i have the animation player node. I added multiple instances of this enemy object in to the main scene.

main scene screenshot
Untitled.png - Google Drive

Enemy game object screenshot

Untitled2.png - Google Drive


muralis923 | 2021-01-21 07:55

:bust_in_silhouette: Reply From: Andrea

i’m not sure because i dont know how you arranged the enemy scene,but I can see 2 reasons:

1) you are calling ConfigAnimation only at the beginning: if that’s the case you are freezing the starting position into P, therefore everytime the animation player starts it brings back the enemy into P before making it topple.

2) there is a “bug” in the animationplayer: when referring to the property of a node to animate, sometimes it write down the path comprhensive of the Node name. Eg: if you animate the position of the sprite in this configuration


if you click on the the animation track name it will show .../Node2D/Sprite:position:y: this does not work for multiple instance of Node2D, as they would have different names, so each animation player will always animate only the first instanced object.
Change it toSprite:position:y

Anyway, you are using the animation node in a complex way, it would be much easier if you topple the mesh node only, instead of the entire enemy (like in my example with the sprite).
in this way you can set up the animation node directly in the editor, because the translation would be relative to the parent (the enemy node), so P would aways be Vector3(0,0,0) and P1 Vector3(0,2,0), regardless of the enemy node actual position

Thanks Andrea. I called the config animation method in the func _ready() method of the enemy game object script.
Then I changed it to be called just after the player bullet hit the enemy. It worked as expected.



muralis923 | 2021-01-21 09:27