Hello! I have a simple question. I want an enemy to run after a player but stop once it gets close. I am currently using MoveTo and trying to move to the position of the targets root part. The issue is I want to to work from all angles.
Currently it is trying to run inside of the player.
You first need to get a LookVector from a CFrame of the NPC looking at the player. Then you call MoveTo, moving the NPC to the player while subtracting a multiple of the LookVector.
local waypointSpacing = 4 -- how many studs back the NPC will stop before the waypoint
-- get the required info
local playerCF = randomCharacter.PrimaryPart.CFrame
local npcPos = NPC.PrimaryPart.Position
-- get the look CFrame
local lookCF = CFrame.lookAt(npcPos, playerCF.Position)
-- subtract the lookvector from the target cframe
local moveTarget = playerCF - (lookCF * waypointSpacing)
NPC.Humanoid:MoveTo(moveTarget.Position)
1 Like
local stoppingSpace = 2
local offset = target.Position - NPC.Position
offset = offset - (offset.Unit * stoppingSpace)
humanoid:MoveTo(NPC.Position + offset)
the smaller stoppingSpace is the closer the npc will move to target
2 Likes