Rotation based on amount of bullets (2D)

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

I’m looking to rotate the bullets being shot based on the amount, I’m not sure how to do so (ex. if I chose 3 for bullet amount, it’d shoot like a shotgun in a cone). I’m new to godot so sorry If there’s a very easy and clear answer that I should know about already.

My function:


#spawning
for i in info["bullet_amount"]:
	var bullet = bullet_path.instance()
	bullet.position = $Position2D.global_position
	bullet.rotation = self.rotation #rotate base on amount
	get_parent().get_parent().add_child(bullet)
#moving
	bullet.speed = info["bullet_speed"]
	bullet.velocity = get_global_mouse_position() - bullet.position #moves in another script
:bust_in_silhouette: Reply From: exuin

presumably something like this? where spread is the total rotation of the bullets

bullet.rotation = rotation + spread / info.bullet_amount - spread / 2

I played around with it a little but I can’t get spread to equal the total rotation of the bullets, here’s my code so you can show me what I’m doing wrong:

#spawning
for i in info["bullet_amount"]:
	var bullet = bullet_path.instance()
	bullet.position = $Position2D.global_position
	var spread = 0 + bullet.rotation # no idea what to do here
	bullet.rotation = self.rotation + spread / info["bullet_amount"] - spread / 2
	var direction = bullet.global_transform.basis_xform(Vector2.RIGHT)
	get_parent().get_parent().add_child(bullet)
#moving
	bullet.speed = info["bullet_speed"]
	bullet.velocity = direction

Mischa | 2022-11-08 02:57

spread should be outside the for loop and should be the total spread of the bullets (pi/4 rad, pi/2 rad, etc)

exuin | 2022-11-08 03:25