CFrame math problem

  1. What do you want to achieve? Keep it simple and clear!
    I would like to make a non primary part inside a model point at another part while maintaining the same primary part position and same offset to the primary part.

  2. What is the issue? Include screenshots / videos if possible!
    I cannot figure this out

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried searching the devforum

here is an example
so here is what it is like now:
image
here is what i want it to be:
image
as you can see the gray parts position did not change, it retained the same rotational and positional offset to the green part, and the green part is facing the red part.

What is the context? Is it required for them to be within the same model?
If not, the simple solution would to have it be make it an independant part.

It’s just more convenient for me for the gray and green parts to be in the same model.

Can’t you just set the part that you want to change’s CFrame?

local part = workspace.Model.Part
local lookAtPart = workspace.Part2

part.CFrame = CFrame.lookAt(part.Position, lookAtPart.Position)

i want them to keep the same offset

Oh I think I get what you mean.

You can just get the gray part’s CFrame relative to the green’s CFrame using :ToObjectSpace then call :ToWorldSpace after changing the orientation of the green part. So like:

local grayPart = workspace.Model.GrayPart
local greenPart = workspace.Model.GreenPart

local grayCFrameRelativeToGreenCFrame = greenPart.CFrame:ToObjectSpace(grayPart.CFrame) -- sorry for the long name lol

greenPart.CFrame = CFrame.lookAt(greenPart.Position, workspace.RedPart.Position)
grayPart.CFrame = greenPart.CFrame:ToWorldSpace(grayCFrameRelativeToGreenCFrame)
1 Like

this works only it does not retain the gray parts position

I’m a bit confused now, you want the green part to move independently of the gray part? You should be able to just assign the green part’s CFrame property using CFrame.lookAt without :PivotTo.

i want the gray part to pivot so the green part is facing the red part but the gray part does not move

Okay then yeah, this post should work.

they don’t keep the same rotational offset though

I’m still confused, sorry. You want the gray part to maintain only the orientation relative to the green part?

Try this:

local grayPart = workspace.Model.GrayPart
local greenPart = workspace.Model.GreenPart

local grayCFrameRelativeToGreen = greenPart.CFrame:ToObjectSpace(grayPart.CFrame)
greenPart.CFrame = CFrame.lookAt(greenPart.Position, workspace.RedPart.Position)

grayPart.CFrame = CFrame.new(grayPart.Position) 
* CFrame.Angles(grayPart.CFrame:ToWorldSpace(grayCFrameRelativeToGreen):ToOrientation())

If that still isn’t what you want, could you send a photo of what your goal result looks like?

so here is what it is like now:
image
here is what i want it to be:
image
as you can see the gray parts position did not change, it retained the same rotational and positional offset to the green part, and the green part is facing the red part.

2 Likes

the proper way to phrase this is “i want the gray part to maintain the same position and orientation relative to the green part”

i would just create a sub group and make a model of the green and gray part with the green being the primary part. Then use the Model:PivotTo() function to rotate the green part. Thisll rotate the gray part about the green part keeping its relative transform

this would not keep the gray parts position

Here Just coded this, it might help you
lookat.rbxl (41.4 KB)

this does not keep the same rotational and positional offset though

You want a snapping kinda thing? like 90degree interval of rotation?

use the same method i mentioned earlier however instead make the gray part the primary part. Use this line of code. Model is the model containing the gray and green part with gray as the PP

local offset = gray.Position - green.Position
Model:PivotTo( cf.LookAt( gray.Position, red.Position + offset)