#npc looking at you

1 messages · Page 1 of 1 (latest)

fallen vault
#

I need help knowing how to make the npc head look at you when you approach it

teal lichen
#

There are two parts to this, distance detection and the looking at part. The looking at part is easy, there's a function CFrame.lookAt(at, lookAt) which returns a CFrame which is set so that at is looking at lookAt. Here's an example of that.

local Players = game:GetService("Players")
local cube = script.Parent.WireframeCube
cube.PrimaryPart = cube:FindFirstChild("Cube.001")
local cf = cube:GetPrimaryPartCFrame()
-- local rotation = CFrame.Angles(0, math.pi/32, 0)
Players.PlayerAdded:Connect(function(player)
  player.CharacterAppearanceLoaded:Connect(function(character)
    while true do
      cf = CFrame.lookAt(cf.Position, character.PrimaryPart.Position)
      cube:SetPrimaryPartCFrame(cf)
      task.wait(0.02)
    end
  end)
end)

For the distance detection, I would recommend making an invisible part that surrounds the npc, that detects whenever you leave and enter the part. When you're inside, it connects to an event that makes the npc head look at you. When you leave, it disconnects that event.

fallen vault