Godot Version
4.2.2
Question
Hi there, I’m rather new to Godot and GameDev in general, so I’m probably missing something obvious. I’m trying to connect segments of rope (RigidBody2D with a Line2D and a CollisionShape2D) together with PinJoint2Ds. I did two segments in the editor and those work fine. Then I tried to automate the process for additional segments and those simply fall off. What am I missing? Thanks a lot for any help!
(The pin_joint_01.position and pin_joint_02.position are simply the top and bottom of the folded up rope. It’s 0/-1 and 0/-7.)
extends Node2D
var rope_segment_scene = preload("res://scenes/rope_segment.tscn")
@onready var pin_joint_01 = $PinJoint_01
@onready var pin_joint_02 = $PinJoint_02
func _ready():
var last_connected_segment = 2
for i in range(0, 10):
var pinjoint_top = PinJoint2D.new()
pinjoint_top.position = pin_joint_02.position
var pinjoint_bottom = PinJoint2D.new()
pinjoint_bottom.position = pin_joint_01.position
var rope_segment_down = rope_segment_scene.instantiate()
rope_segment_down.position = pinjoint_top.position
add_child(rope_segment_down)
rope_segment_down.name = "Segment_0" + str(last_connected_segment + 1)
var rope_segment_up = rope_segment_scene.instantiate()
rope_segment_up.position = pinjoint_top.position
add_child(rope_segment_up)
rope_segment_up.name = "Segment_0" + str(last_connected_segment + 2)
pinjoint_top.node_a = get_node("Segment_0" + str(last_connected_segment)).get_path()
last_connected_segment += 1
pinjoint_top.node_b = get_node("Segment_0" + str(last_connected_segment)).get_path()
pinjoint_bottom.node_a = get_node("Segment_0" + str(last_connected_segment)).get_path()
last_connected_segment += 1
pinjoint_bottom.node_b = get_node("Segment_0" + str(last_connected_segment)).get_path()