Need help with shooter npc

I’m making a 3rd person shooter game and right now I’m working on Enemy NPC. While I was researching on how to make good shooter npc i’ve stumbled upon this post : [https://x.com/i/status/1604194301097005057] but I’ve been struggling and now stuck.

The problems that I am facing right now are:

  1. Cover and peek out to shoot (Biggest problem)
  • I actually kind of achieved that but as soon as the target’s position changed the npc would just chase the target and tried to find cover at the same time resulting in them just going back and forth toward the player and cover. I honestly have no idea how to fix that.
  1. Pathfinding
  • I use Roblox’s pathfinding service for my npc but it’s rather slow because every time we move to a waypoint we have to do Humanoid.MoveToFinished:Wait() which is not good for chasing players and also getting it to work with cover and peek system is a pain.

Right now I am very stuck. I hope some of you may give or guide me the answer.
This is my full code right now.

local PathfindingService = game.PathfindingService

local NPC = workspace.Characters:WaitForChild("Enemy")
local HumanoidRootPart = NPC.HumanoidRootPart
local Humanoid = NPC.Humanoid

local RangeFromTarget = 50
local SideRayLength = 1.1
local Target = workspace:WaitForChild("Player")

local raycastfilter = RaycastParams.new()
raycastfilter.FilterType = Enum.RaycastFilterType.Exclude
raycastfilter.FilterDescendantsInstances = {NPC,Target}

local clamp = math.clamp
local rad = math.rad
local abs = math.abs

local ChangeOffset = false
local path_update = -math.huge
local WalkOffset = Vector3.new(500,0,100)

local function getPath(destination)
	local path = PathfindingService:CreatePath()

	path:ComputeAsync(NPC.HumanoidRootPart.Position, destination)

	return path
end

local function pathFindTo(destination)
	local path = getPath(destination):GetWaypoints()
	if path[2] then
		local lastpoint = path[2]
		return lastpoint.Position
	else
		return HumanoidRootPart.Position
	end
end

local CoverTme = 0
local CoverPos = nil
local CoverOffset = 0

local FromWallOffset = 2.4

function checkSight(target)
	local ray = Ray.new(NPC.PrimaryPart.Position, (target.Position - NPC.PrimaryPart.Position).Unit * 275)
	local part,position = workspace:FindPartOnRayWithIgnoreList(ray, {NPC})

	if part then
		if part:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end

local BG = Instance.new("BodyGyro")
BG.MaxTorque = Vector3.new(0,0,0)
BG.CFrame = HumanoidRootPart.CFrame
BG.Parent = HumanoidRootPart

local Peeking = false
local FindingCover = false
local TakeCover = false
local FireTick = -math.huge
local Ammo = 25

game["Run Service"].Heartbeat:Connect(function(dt)
	local OffsetFromTarget = HumanoidRootPart.CFrame:ToObjectSpace(Target.PrimaryPart.CFrame)
	
	for x = -1,2 do
		local ray = workspace:Raycast(HumanoidRootPart.Position,HumanoidRootPart.CFrame.RightVector * (SideRayLength * x),raycastfilter)
		if ray then
			HumanoidRootPart:ApplyImpulse(HumanoidRootPart.CFrame.RightVector * -10 * x)
		end	
	end
	
	local strafe_dir = math.sign(OffsetFromTarget.X+1)
	local CF = CFrame.new(HumanoidRootPart.Position,Vector3.new(Target.PrimaryPart.Position.X,HumanoidRootPart.Position.Y,Target.PrimaryPart.Position.Z))
	
	if checkSight(Target.PrimaryPart) and not TakeCover then		
		
		BG.MaxTorque = Vector3.new(1,1,1) * 999999		
		BG.CFrame = CF

		if not Peeking then
			Peeking = true
			
			coroutine.wrap(function()
				Humanoid:MoveTo(
					pathFindTo(CF*CFrame.new(-strafe_dir*10,0,0).Position)
				)
			end)()
		end
		
		if Ammo <= 0 then
			TakeCover = true
		end
		
		if tick() - FireTick > 0.1 then
			FireTick = tick()
			Ammo -= 1
			print("Fire!")
		end
	elseif TakeCover == true and Ammo <= 0 then
		BG.MaxTorque = Vector3.new(0,0,0)
		if not FindingCover then
			FindingCover = true
			
			Humanoid:MoveTo(
				pathFindTo(
					(CF * CFrame.new(strafe_dir*25,0,25)).Position
				)
			)
		end
	else
		Peeking = false
		BG.MaxTorque = Vector3.new(0,0,0)
		Humanoid:MoveTo(
			pathFindTo(Target.PrimaryPart.Position)
		)
	end
end)

Hello :waving_hand: even though im not the best at pathfinding i did find a nice pathfinding service you could use! NoobPath | Easy Pathfinding
Goodluck :+1:

I have several advices how to solve your problems you mentioned:

  1. To solve the first problem, I recommend you to use state manager. I’ll tell a store as example:

NPC detecting target and shoot him. After target left NPC’s field of view (you can use same raycasting system I said in the second answer), state manager tells NPC to compute path using PathfindingService (as finish target I recommend you to use position, where NPC last time saw target). After path was finished, you NPC starts to function normally. If his target ran out of point NPC saw him last time, means NPC left the target and start looking for another one.

  1. To solve the second problem, I recommend you to use additional raycasting, directed from NPC’s root part to it’s part root part. If raycasting returned result, you can use normal Humanoid:MoveTo() method, but keep in mind that method cannot make NPC jump, climb, etc.

P.S: In this post a said a few time state manager. With this phrase I meant not only hard system like HumanoidState, but a simple algorithms that fires functions, resumes coroutins. Try to find the easiest and the most optimized way to solve your problem.