So i have a npc and i want the head to look at the player, unless his behind him how could i do this?
You might wanna study some basic scriptings first before you even make a short post like this.
You could consider using CFrames to make the head look at the player, and about the exception part, you have to use Vector:Dot()
function in order to check how much are the head and the player’s head looking at the same direction, then use your logic to achieve your goal.
You could make a part and if the player is touching it make the head rotate where the player is at and make that part in front of the npc if you want it not to look behind it
I tried but this is the result i get
local Character = script.Parent
local Hitbox = Character.Head
local RayCastP = RaycastParams.new();
RayCastP.FilterType = Enum.RaycastFilterType.Blacklist
local Closet_HeadFollow = function()
for _, Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
if Player:DistanceFromCharacter(Hitbox.Position) < 100 then
Character.Head.CFrame = CFrame.new(Character.Head.Position, Player.Character.HumanoidRootPart.Position)
end
end
end
end
while task.wait() do
Closet_HeadFollow()
end
Maybe just use some mathematics to calculate the distance instead of using a function?
i want the head to move not the whole body
Could you try to set the head’s CFrame
to something else just so to make sure the whole body doesn’t get teleported when doing that?
Let me know if the whole body still teleports or affected by it.
function findHead(pos)
local torso = nil
local dist = 200
local child = workspace:children()
for i=1, #child do
if child[i].className == "Model" then
local h = child[i]:findFirstChild("Humanoid")
if h ~= nil then
local check = child[i]:findFirstChild("UpperTorso")
if check ~= nil then
if (check.Position - pos).magnitude < dist then
torso = check
dist = (check.Position - pos).magnitude
end
end
end
end
end
return torso
end
while true do
wait()
local torso = findHead(script.Parent.Position)
if torso ~= nil then
script.Parent.CFrame = CFrame.new(script.Parent.Position, torso.Position)
end
end
or
function findHead(pos)
local head = nil
local dist = 200
local child = workspace:children()
for i=1, #child do
if child[i].className == "Model" then
local h = child[i]:findFirstChild("Humanoid")
if h ~= nil then
local check = child[i]:findFirstChild("Torso")
if check ~= nil then
if (check.Position - pos).magnitude < dist then
head = check
dist = (check.Position - pos).magnitude
end
end
end
end
end
return head
end
while true do
wait()
local head = findHead()(script.Parent.Position)
if head ~= nil then
script.Parent.CFrame = CFrame.new(script.Parent.Position, head.Position)
end
end