I want an npc to look at a player when a ProximityPrompt is triggered, but I have no idea how to do this. I was thinking of using CFrame.lookAt but I dont know how to do that.
You can do NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, targetPos, NPC.HumanoidRootPart.CFrame.UpVector
and set targetPos
to the position of the other character.
Make sure everything is unanchored (except for the HumanoidRootPart, if necessary to anchor it).
would this be in a local script or script and where would the script be?
also how would I define the player for targetPos
This should probably be in a ServerScript if you want it to look at the closest person
This should work but I havent tested it
local Players = game:GetService('Players')
local NPC = -- Path to NPC
while task.wait() do
local list = {}
for _, plr in Players:GetPlayers() do
if plr.Character then
local p1 = plr.Character.HumanoidRootPart.Position
local p2 = NPC.HumanoidRootPart.Position
local dist = (p2 - p1).Magnitude
list[tostring(dist)] = plr
end
end
local leastdistance = math.huge
for d,_ in list do
if tonumber(d) < leastdistance then
leastdistance = tonumber(d)
end
end
local plr = list[tostring(leastdistance)]
if plr and plr.Character then
local HRP:BasePart = plr.Character.HumanoidRootPart
local Goal = CFrame.lookAt(
NPC.HumanoidRootPart.Position,HRP.Position,HRP.CFrame.UpVector
)
NPC.HumanoidRootPart.CFrame =
NPC.HumanoidRootPart.CFrame:Lerp(Goal,.2)
end
end
Also, make sure to ONLY anchor the HumanoidRootPart, don’t anchor anything else as im pretty sure that will make the welds in the NPC break.
I’m sorry, I cant test this because whenever I try to play on the studio it crashes.
It’s ok, I just tested it a few minutes ago and yes this does work.
This works, but how do I fix the npc looking up
(This screenshot is in 1st person, I am on top of the npc)
Don’t change the Z orientation. Or do it the easy way, and change the Z orientation to 0. If you’re using @calvin_coco 's code, the easy way is to add NPC.HumanoidRootPart.Orientation = Vector3.new(NPC.HumanoidRootPart.Orientation.X, NPC.HumanoidRootPart.Orientation.Y, 0)
.
How would I connect it to their code?
Try:
local noZPos = Vector3.new(NPC.HumanoidRootPart.Position.X, NPC.HumanoidRootPart.Y, 0)
local noZLookAt = Vector3.new(HRP.Position.X, HRP.Position.Y, 0)
local goal = CFrame.lookAt(noZPos, noZLookAt)
NPC.HumanoidRootPart.CFrame =
NPC.HumanoidRootPart.CFrame:Lerp(goal,.2)
To replace the whole thing?
This text will be blurred
No, just the part with the Goal
variable. I just renamed it to goal
.
if plr and plr.Character then
local HRP:BasePart = plr.Character.HumanoidRootPart
-- Here
end
@calvin_coco’s code, but modified.
local Players = game:GetService('Players')
local NPC = -- Path to NPC
while task.wait() do
local list = {}
for _, plr in Players:GetPlayers() do
if plr.Character then
local p1 = plr.Character.HumanoidRootPart.Position
local p2 = NPC.HumanoidRootPart.Position
local dist = (p2 - p1).Magnitude
list[tostring(dist)] = plr
end
end
local leastdistance = math.huge
for d,_ in list do
if tonumber(d) < leastdistance then
leastdistance = tonumber(d)
end
end
local plr = list[tostring(leastdistance)]
if plr and plr.Character then
local HRP:BasePart = plr.Character.HumanoidRootPart
local Goal = CFrame.lookAt(NPC.HumanoidRootPart.Position,(HRP.Position * Vector3.new(1,0,1)) + (NPC.HumanoidRootPart.Position * Vector3.new(0,1,0)),HRP.CFrame.UpVector)
NPC.HumanoidRootPart.CFrame =
NPC.HumanoidRootPart.CFrame:Lerp(Goal,.2)
end
end
Try this and it should work correctly.
This works! Also, what If I wanted the player to face at the npc too?, and how do I stop the npc from looking at the player after a certain amount of time
you would have the same script but for the players, and you would basically add a cooldown for the time.
I’d also like to point out that this is very clean. Thank you to @SubtotalAnt8185, @calvin_coco and everyone else who helped me with this.