How to I made r6 NPC head look at player?

So I took a player head movement script and tried to implement it into my NPC like so:

local neck = torso.Neck
local direction = HRP.CFrame:toObjectSpace(head.CFrame).lookVector
	
neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(3 * math.pi/2, 0, math.pi) * CFrame.Angles(0, 0, -math.asin(direction.x)) * CFrame.Angles(-math.asin(direction.y), 0, 0)

However it doesn’t work. The head is just facing the left without moving at all. The player movement system works completely fine though, which is what really confuses me. How can I achieve accurate head movement?

2 Likes

Your saying HRP and head like it’s a var also if you using inheritance for the head var make sure it capitalized.

I already declared the variables I just didn’t show them in the example. There are no output errors for the code so the problem just has to do with the positioning and offset of the neck.

Oh well then I don’t know sorry!

Figured it out!!

local dist = (pos - HRP.Position).Magnitude
	local dir = (pos - HRP.Position).Unit
	local vecA = Vector2.new(dir.X, dir.Z)
	local vecB = Vector2.new(HRP.CFrame.LookVector.X, HRP.CFrame.LookVector.Z)
	local dotProd = vecA:Dot(vecB)
	local crossProd = vecA:Cross(vecB)
	local angle = math.atan2(crossProd, dotProd)
	
	local ht = pos.Y - HRP.Position.Y
	local UDA = math.atan(ht/dist)
	
	torso.Neck.C0 = CFrame.new(torso.Neck.C0.Position) * CFrame.Angles(UDA, angle, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(-180))

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