Place part relative to other part?

Hi, how do i place a part relative to another parts position and rotation? For example I want to place a part 2 studs in front, 3 studs right and 1 stud up relative to another parts position and rotation, and then weld it there.

I know it has something to do with lookvector, rightvector and upvector, but i dont know how to use them to do the stuff that i want to do

5 Likes

You can do it with CFrame
Part2.CFrame = Part1.CFrame * CFrame.new(2,3,1)

6 Likes

I believe you can just multiply the other part’s CFrame by the offset you want, and it will work with the other part’s relative space.

For example:

local new_cframe = start_cframe * CFrame.new(Vector3.new(10,10,10))
-- 10 studs up, right and forward according to the start_cframe's rotation and position
5 Likes

Part2.CFrame = Part1.CFrame*CFrame.new(3, 1, -2)

1 Like

Part2.CFrame = Part2.CFrame:toWorldSpace(Part.CFrame + Vector3.new(translation vectors))

I thhhhhhhhhhink

thanks guys! but how do i not only offset the position, but also the rotation?

Add *CFrame.Angles(math.rad(0),math.rad(0),math.rad(0)) to the end

You can set those 0’s to various values to see how they react. You can also add

CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))

to the front to rotate around a position then move the part.

3 Likes

Going to summarize all your replies into one reply, to use it as answer:

local new_cframe = start_cframe * CFrame.new(Vector3.new(10,10,10)) * CFrame.Angles(Vector3.new(math.rad(50),math.rad(50)math.rad(50)))
-- 10 studs up, right and forward according to the start_cframe's rotation and position
-- and then 50 degrees turned on x,y and z axis
10 Likes

Just do
local new_cframe = start_cframe * CFrame.new(10,10,10) * CFrame.Angles(Vector3.new(math.rad(50),math.rad(50)math.rad(50)))

6 Likes

thats what i just said but okay lol. marked your reply as the answer now.

2 Likes

Read up about CFrames. They’re one of the most useful things in the universe.

Yeah I wanted to, but I just didnt quite understand how to do this.

You added a Vector3.new() inside the CFrame.new wich isn’t needed

5 Likes

I wrote a little function to make this easier, because I position new parts a lot, and I needed a universal tool.
This function will cover all your displacement scenarios.
You will need to write a bit more code to add any rotation though.

It’s may not be that clear how it works by reading the code, so here’s the gist:

Let’s just consider placement along the X-axis for simplicity.
You have a parent block and a child block.
Here are the parameters you need to consider:

  1. Do you want the offset distance to be measured from the parent’s left edge or right edge?
  2. Do you want the offset distance to be measured to the child’s left edge or right edge?

Example:
Suppose you have a parent part, and you want the child to be positioned to the right of the parent, but offset by 3 studs.
This means you want to measure 3 studs from the parent’s right edge, and position the child’s left edge there. (I will just assume you want to the Y and Z faces of the parent and child to be aligned on their edges closest to the origin.)

Here’s how to use my function to position your child part:
(1 means true, -1 means false)

childPart.CFrame = setCFrameFromDesiredEdgeOffset(
                   {
    parent = parentPart,
    child = childPart,
    offsetConfig = {
        useParentNearEdge = Vector3.new(1, -1, -1), -- the 1 here means measure from the X edge farthest from the origin
        useChildNearEdge = Vector3.new(-1, -1, -1),
        offsetAdder = Vector3.new(-3, 0, 0)
    }
})

And here is my function

function setCFrameFromDesiredEdgeOffset(props)
    local parent = props.parent
    local child = props.child
    local offsetConfig = props.offsetConfig

    local defaultOffsetAdder = Vector3.new(0, 0, 0)

    local defaultOffsetConfig = {
        useParentNearEdge = Vector3.new(0, 1, -1),
        useChildNearEdge = Vector3.new(0, -1, 1),
        offsetAdder = defaultOffsetAdder
    }

    offsetConfig = offsetConfig or defaultOffsetConfig

    local offset = (offsetConfig.useParentNearEdge * parent.Size -
                       offsetConfig.useChildNearEdge * child.Size) / 2 +
                       (offsetConfig.offsetAdder or defaultOffsetAdder)

    local newCFrame = CFrame.new(offset)
    return parent.CFrame:ToWorldSpace(newCFrame)
end
4 Likes

One day, I hope to convert this to a plugin, where you can position the parts with a nice UI, and it will spit out the code that you can paste into your script. Kind of like the awesome Tool Grip plugin. One day…

3 Likes

For future reference:

Use an attachment within the part you want any other part to be relative to. Give the relative position. Use the WorldCFrame for the other part, either copying the position manually or using a script that sets the right position.

1 Like