0 votes

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)

asteroidscene is PackedScene, so get instance first, and then update it's position, and finally use addchild 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?

in Engine by (134 points)

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

1 Answer

0 votes

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)
by (385 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.