Smart NPC Chase

  1. What do you want to achieve?
    Smart NPC, like if its chasing a player and he turns left the AI also go left and if plr go to NPC Field Of View and run behind of him it still chasing and not just stop
    I know it would not be a problem if the script used just Magnitude check but I’m checking if plr is in NPC Field of View.

  2. What is the issue?
    Idk how to start coding that nor how I would do it

  3. What solutions have you tried so far?
    I tried waiting sometime before stopping the chase but didn’t work very well, I looked in the devforum for a long time but nothing helped

2 Likes

if you want the npc to stop chasing after a bit you can have a separate thread that has a timer that goes down whenever the player isn’t within the npc’s line of sight. Something kinda like this:

local loseTime = 5
local timeUntilLost = loseTime

task.spawn(function()
	while true do
		if (not playerInSight) and timeUntilLost > 0 then --Put in conditions that count as not seeing the player here
			timeUntilLost -= 0.1
		elseif timeUntilLost > 0 then
			timeUntilLost = loseTime
		end
        if timeUntilLost <= 0 then
			--Stop chase
		end
		task.wait(.1)
	end
end)

as for checking if the player is in sight you can use raycasts (and also the dot product if you’re counting FOV as well)

if you continue chasing the player until the timer expires the npc shouldn’t have any issue losing track of the player if they go behind it as the npc will still track them (and the timer will reset once it gains sight of the player again)

if you’re having trouble working with pathfinding, I recommend SimplePath, which is a module makes handling pathfinding a lot easier:

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.