• Godot HelpGDScript
  • How can I assign a path to follow for a fresh instantiated object via script?

I am spawning new cubes per button click via

var cube = cube_scene.instantiate()
var spawn_location = get_node("Path3D Camera/PathFollow3D")
spawn_location.progress_ratio = randf()
cube.position = spawn_location.position
add_child(cube)

and now I also want it to travel along PathFollow3D - how do I assign that path to the cube?

  • fire7side I am an idiot ... I spawned a CharacterBody3D instead of a RigidBody3D ... now it works 🙂

fire7side Thanks, I have watched the video already but it does any hints regarding assigning my code generated cube to the Path

Ok, figured by just changing the add_child() to

$Path3D/PathFollow3D.add_child(cube)

that this is assigning the cube to the path and makes it follow it.
Thing I noticed: when I assign a cube via the Editor GUI and the path is in the air, the cube drops down to the floor and follows the path. If I add the cube via code, it does not drop down. Do I have to enable gravity somehow for the code generated cubes?

Haven't used it for a long time, but you can probably position the y of the cube to whatever level you want after it's added to the scene.

    fire7side I am an idiot ... I spawned a CharacterBody3D instead of a RigidBody3D ... now it works 🙂