SNPC keeps unaiming/reaiming rather than shoot player
5 replies, posted
I'm attempting to create my first npc that will patrol around and look for the player. Basically whats going on is, when the npc finds the player its supposed to shoot the player, but instead it just keeps aiming/unaiming. Here is a video showing whats going on: Video
Here is the code im using for it to find/target the player:
function npc:Think()
local _ents = ents.FindInSphere(npc:GetPos(),1000)
local foundPlayer = false
for k, v in pairs( _ents ) do
if (v:IsPlayer()) then
npc:AddEntityRelationship(v,D_HT,99)
-- npc:SetEnemy(v) I found this wasn't needed
foundPlayer = true
end
end
if !foundPlayer and npc:GetEnemy() != nil then
npc:ClearEnemyMemory()
npc:SetEnemy(nil)
end
end
Any help is appreciated, ill be more than happy to provide any info you will need!
can you show the shooting part
Im creating the npc from the base 'base_ai' and setting its type to 'ai', I assumed that this already had a shooting function defined similar to how the default npcs work right? If not then I suppose the problem is that I don't have a shooting function defined.
I thought using 'SetEnemy' wasn't needed because it seemed that if I used 'AddEntityRelationship' the ai would still target the player the same.
I've updated the post with some more information to hopefully clarify things.
You're doing your search in Think function. Also, you're doing some strange job there by setting and immediately clearing NPC's enemy on every tick
What would be the proper function to put the npc search in?
I've reworked the code I use for it to find the player, this is what I have:
function npc:Think()
local _ents = ents.FindInSphere(npc:GetPos(),1000)
local foundPlayer = false
for k, v in pairs(_ents) do
if v:IsPlayer() then
if target == nil then
npc:AddEntityRelationship(v,D_HT,99)
npc:SetEnemy(v)
target = v
end
foundPlayer = true
end
end
if !foundPlayer then
if target != nil then
target = nil
end
if npc:GetEnemy() != nil then
npc:ClearEnemyMemory()
npc:SetEnemy(nil)
end
end
end
Now with this code which shouldn't set/clear the npcs enemy every tick the npc looks at me but doesn't do anything. Once it spots me it stops moving and looks at me. I've tried setting its schedule, movement activity and npc state to get it to shoot the player but nothing has worked.
Bump.
Issue still stands and I literally have no idea what to do next. I've looked everywhere for a good example on an snpc that targets the player and I cannot seem to find one.
There are a lot of snpcs on the workshop but the mostly all use vj base and I would not like to have to use that.
Sorry, you need to Log In to post a reply to this thread.