[SOLVED] CFrame.lookat not working

I’m making a script that has a part look at another part but only in one axis.
This is my code:

local lookPart = workspace:WaitForChild("lookPart") -- part that actually does the looking, green part
local lookAtPart = workspace:WaitForChild("lookAtPart") --part to look at, one on ground
local part = workspace:WaitForChild('part') -- part i want the green part to rotate with, the grey one inside green part

while wait() do
	local lookCFrame = CFrame.lookAt(part.Position, lookAtPart.Position)
	local x,y,z = lookCFrame:ToEulerAnglesXYZ()
	lookPart.CFrame = part.CFrame * CFrame.fromEulerAngles(0,0,-y)
end

which works like expected, as shown here:

however, when rotating the part this happens:

as you can see, the part only rotates on the world axis not on the objects realitive axis.

i tried using toobjectspace but i’m not sure where to interject that in.

any help appreciated

1 Like

Try to make look CFrame relative to the part CFrame by adding this.

lookCFrame = part.CFrame:Inverse()*CFrame.lookAt(part.Position`
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.