PcoWSkbVqDnWTu_dm2ix

Position a Part Relative to Another

Position a Part Relative to Another

Mar 30 2021, 5:21 AM PST

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.

Tags:
  • positioning
  • part
  • coding
  • script