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:
- 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.
- 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)