Need help offsetting part relative to position AND rotation of other part

I want part A (yellow) to be offset from part B (red) as part X (green) is offset from part Y (blue).

A mock-up of what I am trying to accomplish:

I have tried using CFrame:ToWorldSpace but it only does half of what I want. It positions the part relative to the position of the other part but not relative to its orientation.

Here is what using ToWorldSpace looks like:

I am not very experienced with CFrames, so I would greatly appreciate any help that you can provide or references to articles that I may have missed which explain how to do this.

2 Likes

What does your code look like right now? All I know is that, this seems like you want the LookVector of the original prats (partB and part Y).

1 Like

I have no code at the moment because I didn’t know what to try other than ToWorldSpace, but I just want to be able to re-position part A every frame. To clarify what I want, if I moved part A to any position, I would want part X to be positioned so that if I put parts B and Y in the same place, facing the same direction, parts A and X would also be in the same position. Now that you mention it, I think LookVector might be useful, but I don’t know exactly how I would use it because I’m not very skilled with CFrames.

Use ToObjectSpace

local Part1, Offset1 = workspace.Part1, workspace.Offset1 -- Blue block with green point of offset
local Part2, Offset2 = workspace.Part2, workspace.Offset2 -- Red block with yellow point of offset

local storedoffset = Part1.CFrame:ToObjectSpace(Offset1.CFrame) -- grab the green points position and rotation relative to blue block

Offset2.CFrame = Part2.CFrame:ToWorldSpace(storedoffset) -- transfer over the stored offset from green as world space for yellow block to use relative to red block

Edit, if you put the last 2 lines in a while task.wait() loop and move the offset1 part you can see the offset2 block move in correlation, it even changes rotation when you rotate the blue block!!

3 Likes

You should also explain to him what ToObjectSpace do, incase he doesn’t know of it.

Well, to get the parts (A and X) to be controlled by parts (B and Y) at real time in the same place, you’ll have to firstly put them in a while task.wait() do.

Then within that loop, you have to get the parts faces.

  • CFrame.LookVector is for getting the front face of the part.
  • CFrame.UpVector is for getting the top face of the part.
  • CFrame.RightVector is for getting the right face of the part.

If you wanted the other side of the part (e.g. Getting the left face of the part), you’d have to firstly get the Vector that is the opposite of the face you want and times it by negative -1.
(Since left is the opposite of right, we’d use CFrame.RightVector * 1)

take note, the 1 can also be interpreted as a stud.

Now with that, all you have to do is make the offset-parts’ CFrame (A and X) be the vectors of the other parts (B and Y), while also having X and Y be copying the same CFrame as A and B. You code would probably look something like this:

local part1, offset1 = workspace.PartB, workspace.PartA -- *
local part2, offset2 = workspace.PartY, workspace.PartX -- *

-- change the paths with an asterisk comment next to them

while task.wait() do

    offset1.CFrame = part1.CFrame.LookVector * -3 -- part A is going to be offsetting from the back of part B.
    offset2.CFrame = part2.CFrame.LookVector * -3 -- same as above, except it's for X and Y.

    part2.CFrame = CFrame.new(Vector3.new(part1.CFrame.Position.X - 5, part1.CFrame.Position.Y, part1.CFrame.Position.Z), part1.CFrame.Rotation) -- making part Y be in the same CFrames as part B, offsetting it on the X axis by -5 studs.
end

The code above is to give you an example of how your code would look like. It may look more messy but it would still get the job done. If they don’t really work out as what you want, you can either manipulate the code to get the results you’re looking for or try out @TenBlocke’s code.

But to sum it all up, I hope this helps you!

2 Likes

Thank you, you have both been very helpful! I understand how Decablocks’ solution works, but I could never have thought of it myself. I really appreciate the easy-to-understand explanations, and this is exactly what I needed!

2 Likes