Godot Version
Godot 4.3
Question
I’m making a game where the player is going through a procedurally generated facility, the code picks from a pool of rooms and snaps the entrance part of the current generating room to the exit of the last one.
The problem comes with the rotation, I can rotate the new room to match the rotation of the last rooms exit, but that rotates the new room along the center, offsetting it, and I want it to rotate along the entrance part so it stays in place.
Entrance is blue, exit is red
Without rotation:
With Rotation (i want it to not be offset and for the entrance to line up with the exit)
Here’s what the model looks like
And here is the code:
extends StaticBody3D
@export var room_count = 100
var room_pool : Dictionary
var rooms : Dictionary
func _ready() -> void:
# Define rooms in pool
room_pool[0] = preload("res://Models/StupidPlatform.blend")
room_pool[1] = preload("res://Models/ExtraStupidPlatform.blend")
generate_rooms()
func generate_rooms():
# Decide which room will be what
for i in room_count:
rooms[i] = get_random_room()
# Generate Rooms
var room_instance
for i in rooms.size():
var prev_room = room_instance
room_instance = rooms[i].instantiate()
if prev_room == null:
# If first room, stay in center
room_instance.global_position = Vector3.ZERO
else:
var prev_exit = prev_room.get_node("Exit")
var entrance = room_instance.get_node("Entrance")
# Find distance between center of model and where the entrance part is
var entrance_offset = entrance.position - room_instance.position
room_instance.position = prev_exit.global_position - entrance_offset
room_instance.rotation = prev_exit.rotation
add_child(room_instance)
func get_random_room():
var random = randi_range(0, room_pool.size() - 1)
var room = room_pool[random]
return room