How would I rotate a model along the Y axis to face another part?

I want to rotate a model along the Y axis (so it always stays vertical) to face another part. However, I don’t understand CFrames well enough to do this. Could someone help me here?

So far, I have this:

while true do
    local cframe = CFrame.new(
	    Sentry:GetPrimaryPartCFrame().Position.X,
    	Sentry:GetPrimaryPartCFrame().Position.Y,
    	Sentry:GetPrimaryPartCFrame().Position.Z
    ) * CFrame.fromEulerAnglesXYZ(0, 
    	CFrame.lookAt(Sentry.PlacePart.Position, TargetEnemy.Torso.Position).Rotation.UpVector.Y,
    	0
    )

    Sentry:SetPrimaryPartCFrame(cframe)
    wait()
end

(also yes I do know that i shouldn’t put this in a while loop and should have a heartbeat connection but I’m just experimenting for now)

(I also know my code is terrible but again I’m just experimenting with this)

Anyone know how I should properly do the CFrame? Thanks.

1 Like

As it turns out, :ToOrientation() exists. (I did try with :toEulerAnglesXYZ() but that didnt give me the results I wanted.)

Here’s the code for anyone interested:

		while true do
			local x, y, _ = CFrame.lookAt(Sentry.PlacePart.Position, TargetEnemy.Torso.Position):ToOrientation()
			
			local cframe = CFrame.new(
				Sentry:GetPrimaryPartCFrame().Position.X,
				Sentry:GetPrimaryPartCFrame().Position.Y,
				Sentry:GetPrimaryPartCFrame().Position.Z
			) * CFrame.fromEulerAnglesXYZ(
				0, 
				y,
				0
			)

			Sentry:SetPrimaryPartCFrame(cframe)
			wait()
		end
1 Like