I’m trying to make an AI that shoots the player when they are within a certain distance.
Here is an example using a graph:
X is where the target (player) is and the circle around it is the “deadzone” that the npc must be inside in order to shoot at the player. How can I achieve this? I wasn’t able to find anything about it on the devforum or youtube unless I have missed something.
You would need to use Magnitude for it and constant checking whether something containing a Humanoid is nearby. I would do it this way:
Initial setup
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10
while task.wait(0.15) do
end
Is something in workspace that has a humanoid nearby?
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10
while task.wait(0.15) do
for _,v in workspace:GetChildren() do
if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
end
end
end
Oh? In that case, let’s make a path to it.
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10
while task.wait(0.15) do
for _,v in workspace:GetChildren() do
if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
local playerHrp = v.HumanoidRootPart
local hrp = script.Parent.HumanoidRootPart
local humanoid = script.Parent.Humanoid
local distance = (playerHrp.Position - hrp.Position).Magnitude
local path = pathfindingService:CreatePath()
path:ComputeAsync(hrp.Position,playerHrp.Position)
local waypoints = path:GetWaypoints()
end
end
end
Right, I need to also make sure to constantly update the distance and actually make it walk to the target. Let’s also ensure to stop whenever it’s approaching too close.
local pathfindingService = game:GetService("PathfindingService")
local spotDistance = 35 --adjust to preference
local stopDistance = 10
while task.wait(0.15) do
for _,v in workspace:GetChildren() do
if v:FindFirstChildWhichIsA("Humanoid") and v.Name ~= script.Parent.Name and (v.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= spotDistance then
local playerHrp = v.HumanoidRootPart
local hrp = script.Parent.HumanoidRootPart
local humanoid = script.Parent.Humanoid
local distance = (playerHrp.Position - hrp.Position).Magnitude
local path = pathfindingService:CreatePath()
path:ComputeAsync(hrp.Position,playerHrp.Position)
function updateDistance()
distance = (playerHrp.Position - hrp.Position).Magnitude
end
local waypoints = path:GetWaypoints()
for _,wPoint in waypoints do
humanoid:MoveTo(wPoint.Position)
if distance < stopDistance then
hrp.Anchored = true
repeat
updateDistance()
task.wait(0.1)
until distance > stopDistance
hrp.Anchored = false
end
end
end
end
end
Is there a way around anchoring the npc’s HRP? Because it keeps stuttering and walking when freezing in place, clashing with my CFrame.lookAt that I have implemented.
As for pathfinding issues, I will deal with them on my own.