Change object's location at instantiated moment

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

I’m trying to instantiate object(scene) and set it’s spawn position at the spawn time. Here’s the code I wrote:

func spawn_asteroid():
    var spawn_position: Vector3 = get_spawn_position()

    var asteroid_instance = asteroid_scene.instance()
    asteroid_instance.transform.origin = spawn_position
    spawned_asteroids.append(asteroid_instance)

    add_child(asteroid_instance)

asteroid_scene is PackedScene, so get instance first, and then update it’s position, and finally use add_child to append current scene’s tree.

It seems fine to me, however when I run the game, the object was spawned at (0,0,0) and then move spawn_position.

I changed it’s position before it appended to scene, but why it still (0,0,0) and right after move to the specified position?

In Unity, change object’s location right after instancing works. In Godot Engine, is this approach invalid?

I’m not totally sure what’s causing this, but if it’s just a looks problem you could have the asteroid invisible in the PackedScene and set it to visible after spawning it? The only ideas I can think of are workarounds like this

andersmmg | 2019-11-08 20:16

:bust_in_silhouette: Reply From: LoneDespair

func spawn_asteroid():

var spawn_position: Vector3 = get_spawn_position()
var asteroid_instance = asteroid_scene.instance()


asteroid_instance.set_position(spawn_position)


spawned_asteroids.append(asteroid_instance)
add_child(asteroid_instance)