Instancing Objects with rotation

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

I have the following codes

extends Area2D

export var num_of_spawn : int = 0
export var ammo_to_be_spawned : PackedScene

func _unhandled_input(event):
	if event.is_action_pressed("left_click"):
		instance_new_ammos()
		
		
func instance_new_ammos():
	var offset = 50
	var dir := 1
	var mulitiplier = 1
	var test_array = []
				
	for i in num_of_spawn:
		var new_ammo = ammo_to_be_spawned.instance()
		var my_position = Vector2.ZERO
		var a_width = new_ammo.get_node("Sprite").texture.get_width() *new_ammo.get_node("Sprite").scale.x
		print(a_width)
		my_position = global_position
		
		if test_array.size() == 0:
			if num_of_spawn % 2 == 0:
				my_position.y += offset* 0.5 * -dir
							
		else :
			my_position = test_array[i-1].global_position
			my_position.y = my_position.y + offset  * mulitiplier  * dir 
			
			mulitiplier += 1
			if dir == 1:
				dir = -1
			else:
				dir = 1	
		
		
		new_ammo.global_position = my_position
		new_ammo.rotation = rotation
		
		get_tree().get_root().add_child(new_ammo)
		test_array.append(new_ammo)

Above works when object is instanced from a default rotation, which is 0 (facing right)
but when I started rotate the parent object and then call this function , objects instanced will not be straight. I kinda know it has something to do with rotation , but I don’t know how to take rotation into account when I instance the object

Update : I just realized I didn’t quite explain my question well enough so here is a brief image of what I have in mind

enter image description here

Left of the image is what I am getting, and right side of the image is what I want. Sorry about the confusion

I don’t understand what’s wrong here. You are instancing ammo globally at a given position, and using the same local orientation as your Area2D because you use rotation. So what you spawn will have the same rotation that your area has relative to its parent.

Zylann | 2019-12-16 13:55

Sorry about the confusion, I just updated my question. Hopefully it will explain better

lowpolygon | 2019-12-17 02:49

:bust_in_silhouette: Reply From: lowpolygon

Ok…I finally figure it out.
I instance the first one and align the rotation accordingly. Then I instance the rest
but when position it , I set the position with local axis y (transform.y) * offset instead of
just setting the position. Here are the codes

extends Area2D

export var num_of_spawn : int = 0
export var ammo_to_be_spawned : PackedScene

func _unhandled_input(event):
	if event.is_action_pressed("left_click"):
		instance_new_ammos()

func instance_new_ammos():
	
	var parent_node = get_tree().get_root().get_node("MainScene")
	var new_ammo = ammo_to_be_spawned.instance()
	var offset : float = 50.0
	var multiplier = 1
	var test_array = []
	#This is the first ammo to be instanced, all other rotation and postiion will be derived from here
	new_ammo.global_position = global_position
	new_ammo.rotation = rotation
	parent_node.add_child(new_ammo)
	test_array.append(new_ammo)
	
	for i in num_of_spawn-1 :
		
		var a_ammo = ammo_to_be_spawned.instance()
		a_ammo.rotation = rotation
		a_ammo.global_position = new_ammo.global_position + new_ammo.transform.y * offset
		
		if (i+1)%2 == 0: #every 2nd index
			multiplier *= -1
			multiplier += 1	
		else:
			multiplier *= -1	
		
		offset = 50 * multiplier 
		
		parent_node.add_child(a_ammo)
		test_array.append(a_ammo)
	
	rotate_ammos(test_array)
		
		
func rotate_ammos(array_of_ammos):
	
	var default_angle = array_of_ammos[0].rotation
	var rotation_offset = deg2rad(30.0)
	var rotation_multiplier = 1
	for i in array_of_ammos.size():
		
		if i > 0 :
			array_of_ammos[i].rotation += rotation_offset 
			if (i+1) %2 == 0:
				rotation_offset += deg2rad(5.0)
				rotation_offset *= -1
			else:
				rotation_offset *= -1	

Rotate_ammos is just a test function which will make instanced objects rotates in a fan like fashion Now all I need to do is to try to optimize the codes