How can I make a part look at me

Hey, i’m looking to make a part look at me.


Problem

I already have the script, but there is a problem with it.
When I go under the part, it’s also rotating the X and Z axis.
And I only want to rotate the Y axis.


Script

Code
--// FUNCTION

local function FindPlayer(position)
	
	local torso = nil
	local distance = 1000
	local child = workspace:GetChildren()
	
	for i=1, #child do
		if child[i].className == "Model" then
			local humanoid = child[i]:findFirstChild("Humanoid")
			if humanoid ~= nil then
				local head = child[i]:findFirstChild("Head")
				if head ~= nil then
					if (head.Position - position).magnitude < distance then
						torso = head
						head = (head.Position - position).magnitude
					end
				end
			end
		end
	end
	return torso
end

----------------------------------------------------------------------------------------

--// FUNCTION CALLS

game:GetService("RunService").Stepped:Connect(function()
	local torso = FindPlayer(script.Parent.Position)
	if torso ~= nil then
		script.Parent.CFrame = CFrame.new(script.Parent.Position, torso.Position)
	end
end)

Video


So how can I make it only rotate Y axis using that script ?
Thanks!

1 Like

CFrame.lookAt(start, end)

but in your case you wanted to lock the x/z axis you could do

CFame.lookAt(start, Vector3.new(end.X, start.Y, end.Z))

in order to only rotate something by its Y-axis:

local function CopyYAxis(Base: CFrame | Vector3, ToCopy: CFrame): CFrame
	local _, Y = ToCopy:ToOrientation()
	return CFrame.new(Base.X, Base.Y, Base.Z) * CFrame.Angles(0, Y, 0)
end

local C0 = script.Parent.CFrame
local C1 = CFrame.new(C0.Position, torso.Position)
script.Parent.CFrame = CopyYAxis(C0, C1)
1 Like

Thanks a lot! Works perfectly fine

1 Like

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