Hello! I’m making a market system in a game which has a NPC that is supposed to look at you once you come within a certain amount of studs of him. Here is the current code for turning the head to face you:
local boothPerson = game.Workspace.Dummy
local NPCHRP = boothPerson.HumanoidRootPart
local neck = boothPerson.Head:WaitForChild("Neck")
local xOffSet = neck.C0.X
local yOffSet = neck.C0.Y
local zOffSet = neck.C0.Z
function main()
local HRP = script.Parent:WaitForChild("HumanoidRootPart")
local dist = (HRP.Position-NPCHRP.Position).Magnitude
local dir = (HRP.Position-NPCHRP.Position).Unit
local vecA = Vector2.new(NPCHRP.CFrame.LookVector.X, NPCHRP.CFrame.LookVector.Z)
local vecB = Vector2.new(dir.X, dir.Z)
local dotValue = vecA:Dot(vecB)
local crossValue = vecA:Cross(vecB)
local ht = NPCHRP.Position.Y - HRP.Position.Y
local upAngle = math.atan(ht/dist)
local angle = math.atan2(crossValue, dotValue)
if angle > math.pi/3 then
angle = math.pi/3
elseif angle < -math.pi/3 then
angle = -math.pi/3
end
neck.C0 = (CFrame.new(xOffSet, yOffSet, zOffSet)*CFrame.Angles(0,-angle,0)*CFrame.Angles(-upAngle, 0, 0))
end
RunService:BindToRenderStep("standHeadFollow", 1000, main)
The odd thing is this code works perfectly fine on an R15 dummy, but not on an R6 dummy, which is a problem because I wanted to make the NPC R6. Here’s two videos showing the difference:
(Sorry in advance that it’s low quality, this isn’t my regular setup so I don’t have OBS installed)
I’m not sure what causes the difference but thanks in advance if anyone can fix it!
This problem was never solved but if anyone is having this issue and wants a fix that’s relatively close to what I was trying to accomplish, here’s some code that makes the head look side to side where the player goes, but not up or down:
Code
function main()
local HRP = script.Parent:WaitForChild("HumanoidRootPart")
local dist = (HRP.Position-NPCHRP.Position).Magnitude
local dir = (HRP.Position-NPCHRP.Position).Unit
local vecA = Vector2.new(NPCHRP.CFrame.LookVector.X, NPCHRP.CFrame.LookVector.Z)
local vecB = Vector2.new(dir.X, dir.Z)
local dotValue = vecA:Dot(vecB)
local crossValue = vecA:Cross(vecB)
local ht = NPCHRP.Position.Y - HRP.Position.Y
local upAngle = math.atan(ht/dist)
local angle = math.atan2(crossValue, dotValue)
if angle > math.pi/3 then
angle = math.pi/3
elseif angle < -math.pi/3 then
angle = -math.pi/3
end
neck.C0 = (CFrame.new(xOffSet, yOffSet, zOffSet)*CFrame.Angles(0,-angle,0)*CFrame.Angles(-upAngle, 0, 0))
end
RunService:BindToRenderStep("standHeadFollow", 250, main)