![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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
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