Code to spawn multiple objects in a circle spawns them all in one direction

Godot Version

4.3 stable Windows 11

Question

I have this node that is supposed to spawn hammer objects in a circle with amount defined by a “howmany” variable:

                var howmany = amount_r
		var dir = get_angle_to(get_global_mouse_position())
		for i in howmany :
			var hammer = hammer_scene.instantiate()
			hammer.angle = dir+(TAU*i)/howmany
			print("hamer angle should be:", dir+(TAU*i)/howmany)
			add_child(hammer)

For the hammer object itself, I have an animation player to make it swing in an arc, and code to set its angle:


func _ready():
	print("hammer angle is:", angle)
	$AnimationPlayer.get_animation("new_animation").track_set_key_value(0,0,deg_to_rad(0) + angle)
	print($AnimationPlayer.get_animation("new_animation").track_get_key_value(0,0))
	$AnimationPlayer.get_animation("new_animation").track_set_key_value(0,1,deg_to_rad(120) + angle)
	$AnimationPlayer.get_animation("new_animation").track_set_key_value(0,2,deg_to_rad(110) + angle)

As far as I can tell, this code should create a full circle of evenly spread hammers. In all my print functions, everything is as it should be, the angles are always correct and different for every hammer object. However, in game, they are all spawning with the exact same rotation perfectly on top of each other - or maybe it’s only spawning one, it’s hard to tell.

I’ve been looking at this for a while now and I really don’t see the problem, especially with the print functions returning everything right. What could be the issue? Any help is appreciated.

Your animation screenshot has a rotation track for the hammer. Is that perhaps stomping the rotation you’ve set in code?

I don’t think so, because with only one hammer, it works fine - it follows the mouse just like the code tells it to. And anyways, the code is what sets the value of the rotation track keys to begin with.

Just to be sure, you might want to uncheck the rotation animation in the player and see what happens.

I did that, and the result is it doesn’t rotate at all, and always spawns in the same direction.

Try setting rotation instead of angle, perhaps?

Now they do spawn in a proper circle, but due to the disabled rotation track in the animation mixer, they don’t do the swing animation.

Maybe if you re-enable the track it’ll work properly?

Nope, now all the hammers spawn and swing in the same direction every time.

Does it work if you set global_rotation instead?

Same result as before.

Well, a potential fix, I guess, would be to add a Node2D child, set the rotation of that child for your circle facing, and then add the animated hammer as a child of the Node2D. So it would look like:

thing
    node2d, rotated one step
        hammer
    node2d, rotated two steps
        hammer
[...]

That would keep the rotation anim in the hammer from interfering with the rotation you set, since the rotation you set would be in the hammer’s parent node.

That does fix it, yes, but I worry about performance issues due to too many nodes - my game has a lot of things on screen at any given time.

But I can use it as a possibly-temporary solution until I find something better. Thank you for the help!

No problem. Good luck!