Position a Part Relative to Another
Position a Part Relative to Another
Problem
You want to position a part relative to another part.
Solution
Use the CFrame:toWorldSpace
method.
local part = Instance.new('Part') part.Parent = Workspace part.Anchored = true part.CFrame = CFrame.new(0, 5, 0):toWorldSpace(Workspace.Part)
Discussion
In this code we set a newly created part’s Articles/Understanding CFrame|CFrame
to be 5 studs higher than then CFrame of Workspace.Part
. Say that Workspace.Part
is in the center of the map. We have another position that’s (0, 5, 0). It will be located five studs above Workspace.Part
. However Workspace.Part
is not in the center, it’s somewhere else. When we call CFrame:toWorldSpace
we use what Workspace.Part
, but create a new CFrame that keeps the relativity of CFrame.new(0, 5, 0)
to Workspace.Part
. Resulting in a new CFrame which is five studs higher than Workspace.Part
.