Very new to Godot and to game development in general. I’m trying to throw together a game after going through most of Clear codes 11.5 hour tutorial.
My goal is to spawn different, interchangable parts of a space ship in a line. My question is, how do I spawn 2 (or more) parts join them together and have them move together as one coherent ship.
I’ve managed to spawn them in the correct relative positions but i can’t get them to “stick together” and honestly don’t know where to start
Use a parent node of type Node2D. If you move the parent the children move along.
Node 3D (parent)
|--Part1
|--Part2
|--Part3
@export var parts : Array[PackedScene]
func create_ship():
var space_ship = Node 3D.new()
ship.name = "my crazy space ship"
for part : PackedScene in parts:
var new_part = part.instantiate()
space_ship.add_child(new_part)
get_tree().add_child(space_ship)
Thanks very much for the reply, after some fiddling around I got your code to work, but it seems to be behaving the same way, while both test parts spawn, they aren’t attached to each other, what should the root node of each part scene be? I assumed RigidBody2d but if the parent node I’m attaching the parts to is what I should be moving, maybe thats not the right node type
So if I’m understanding you correctly, the root node of each part scene can be a collision, the sprite can be a child of the collision and when I attach each part to the parent node, it will inherit the collisions from each part?
Correct. The physicsBody2D (RigidBody2D or CharacterBody2D) will just check for any collisionShapes in his children and use them as its collisionShape. The important thing is that a PhysicsBody only checks its children for collision shapes (not the children of his children).
But yeah, you undertood correctly.