Rotating a part towards another part only on the X,Z axis

trying to do something similar to CFrame.lookAt() but wont pivot up or down and dont know how i would do it

1 Like

You can do something like this
part1.CFrame = CFrame.lookAt(part1.Position, (part2.Position - part1.Position) * Vector3.new(1, 0, 1))

2 Likes

tried this but its weird and glitchy
https://gyazo.com/703b05c36f03a2ff68eab0220d0c2b81

part1.CFrame = CFrame.lookAt(part1.Position, (part2.Position - part1.Position) * Vector3.new(1, 0, 1) + Vector3.new(0,part1.Position.Y,0))

still very new to lookAt this makes the player not able to move how do i make the player still able to move but also look at the poubt

Can you show how it is implemented? How often does it run and what are part1 and part2 set to?

its in a while wait() do (temporarily) its making the player look at a character model

How is it implemented though? My seconds two questions were just to specify exactly which parts I care about, but I still don’t know how you have it set up. I think it might be a problem with the parameters.

2nd argument of CFrame.lookat is not relative – it’s a position in world space to look at. Now you’d have to do what the others were attempting, which was to project the target position on the XY plane by not accounting for the y-coordinate:

local direction = (part2.Position - part1.Position) * Vector3.new(1,0,1)
part1.CFrame = CFrame.lookAt(part1.Position, part1.Position + dir)
3 Likes

Well you could just use math.atan2 and get the z and x of the difference between the two parts positions and it creates and angle which you can use to rotate with using CFrame.Angles

local red = script.Parent
local blue = workspace.blue


game:GetService("RunService").Heartbeat:Connect(function()
	local dist = (red.Position - blue.Position)
	local angle = math.atan2(dist.Z,dist.X)
	red.CFrame = CFrame.new(red.Position) * CFrame.Angles(0,-angle,0)
end)
3 Likes

ok this should work

part1.CFrame = CFrame.lookAt(part1.Position, part1.Position + ((part2.Position - part1.Position).Unit * Vector3.new(1,0,1)))

Edit: I just tested it, works fine
https://gyazo.com/a12a6ffcf7e6bf01f568314934d5ce03

3 Likes
Part.CFrame = CFrame.lookAt(Part.Position, Vector3.new(Target.Position.X, Part.Position.Y, Target.Position.Z))