How can you change the position and orientation of an object relative to another

I’m trying to create a room generator that firstly creates cells that acts as a reference for the rooms.
The rooms will use the cells’ position and orientation to load the content inside the room to the proper position relative to the cell and a cell inside the room model.

For example, in the two images below :

In the image on the left is a (poorly made) hallway and room example.
The hallway has the white square which acts as a cell that the white square connected to the room (red) will use to position and rotate contents inside.
In the image on the right is the outcome I would want to happen. (The room being rotated and positioned accordingly to the cell’s cframe.)

A simpler example would be seen in the images below:


Where, square 1 (1) acts as the reference point for square 2 (2) since square 1 and the reference (R) are exactly the same (just rotated and moved).
The second image would be the desired outcome where the clone of square 2 would be orientated and positioned differently but correctly in reference to square 1.

I’ve tried to look up and think of ways to calculate the orientation difference and apply that to the positioning, but I’ve had no luck. If anyone knows how to achieve something like this then please let me know.

This probably wouldn’t work, but maybe try parenting the other part to the white square and welding them together. Then, set the position of the white squares equal to each other.

1 Like

Is this what you tried to achieve?

1 Like

Yes, that is what I’m trying to achieve.

Here is the script for it:

local rooms = workspace:WaitForChild("Rooms"):GetChildren() -- the rooms that can be generated
local Hallway = workspace:WaitForChild("Hallway")

local function getConnections(model) -- function to find all the connections
	local connections = {}
	
	for _, part in pairs(model:GetChildren()) do
		if part:HasTag("Connection") and part ~= workspace.Hallway.PrimaryPart then
			table.insert(connections,part)
		end
	end
	
	return connections
end


local connections = getConnections(Hallway)
for _, connection in pairs(connections) do
	
	local newRoom = rooms[math.random(1,#rooms)]:Clone() -- get random room
	newRoom:SetPrimaryPartCFrame(connection.CFrame) -- move the model's PrimaryPart to the connection CFrame
	newRoom.Parent = workspace
	task.wait(.1)
end

Basically what I did was have the rooms as models and setting the model’s PrimaryPart to the ConnectionPart, or where the room should connect with the hallway. Then I use :SetPrimaryPartCFrame(), which moves the PrimaryPart to the CFrame you input and the model along with it.

2 Likes

This is what I was looking for and helps me a lot, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.