How to make object rotate to face another object but on only 1 axis?

Basically what i’m doing is just setting the CFrame to face the player, however because of how CFrame.new() works it rotates on all axis’s. I just want it to rotate on the y. I have heard of fromMatrix() but I don’t understand it and I am not sure if that even is the solution.

How do I fix this?

local path = game:GetService("PathfindingService"):CreatePath()
			local offset = char.PrimaryPart.Position + Vector3.new(0,0,10)
			path:ComputeAsync(script.Parent.HumanoidRootPart.Position, offset)
			script.Parent.Head.Neck.C0 = CFrame.new(0,yOffsetNPC,0) * CFrame.Angles(0,0,0)
			local waypoints = path:GetWaypoints()
			for k, v in pairs(waypoints) do
				script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, char.PrimaryPart.Position)
				script.Parent.Humanoid:MoveTo(v.Position)
				script.Parent.Humanoid.MoveToFinished:Wait()
			end
			script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, char.PrimaryPart.Position)
2 Likes

I would replace the y axis from the char.PrimaryPart.Position with the y axis of the NPC. See if that works. Make sure you replace the following line of code in both places. Hopefully that helped!

script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, Vector3.new(char.PrimaryPart.Position.X, script.Parent.PrimaryPart.Position.Y, char.PrimaryPart.Position.Z))
1 Like