• Make an NPC attack a random player
    3 replies, posted
In my gamemode zombies can spawn at random places around the map. They are the normal npc_zombie ( npc_fastzombie and npc_zombine can spawn to ), not a custom SNPC. How could I get each zombie, as soon as it spawns, to head straight for a random player, even if the player is on the other side of the map and out of sight? At the moment I have this in the zombie spawn point's ( krp_zspawn ) init.lua [lua]function ENT:SpawnZombie() -- Stuff that doesn't matter has been snipped local isFast = "zombie" if math.random() < .5 then isFast = "fastzombie" end if math.random() < .1 then isFast = "zombine" end local z = ents.Create( "npc_" .. isFast ) -- snipped z:SetPos( self.Entity:GetPos() ) z:SetAngles( Angle( 0, math.random() * 360, 0 )) z:Spawn() -- snipped if table.Count( team.GetPlayers( 1 )) > 0 then z:SetEnemy( table.Random( team.GetPlayers( 1 ))) end Msg( "Zombie Spawned\n" ) end[/lua] the "z:SetEnemy()" part doesn't seem to have an effect, as the zombies remain still until they directly see or hear the player. I was thinking about making all players emit a constant sound that can be heard by all NPCs, but the sound would be a blank wav file or something so that players aren't annoyed by it. This would tell all the zombies where each player is, but I'm not sure how to go about this. Thanks in a advance!
Hmm. I've never really used SetEnemy before, just GetEnemy. You could try one of two things: 1. Use the "assault" input to make the enemy attack a particular point or target. Here's a function I used for my NPCs: [code]local function DoAssault(time) timer.Simple(time,function() if npc:IsValid() and npc:GetEnemy() then npc:GetEnemy():SetName(ply:SteamID().."assault") npc:Fire("Assault",ply:SteamID().."assault",0) end end) end[/code] It's weird; you have to set their name to be able to assault them. Alternatively, you could try setting the player's name (I don't know if entity.SetName will affect the player's actual name) and try using zombie:SetEnemy(ply:GetName()). Sorry for being rather unhelpful, but these functions are kind of messed up.
I think I see.. So I would add this function to the NPC, and then after using SetEnemy( player ) I would call this function with a chosen delay. I'll test this now, thanks! [editline]02:20PM[/editline] It appears that if you use player:GetName() on a player it returns the same as player:Nick()! This means I can cut out the SetEnemy() part and just simply use npc:Fire( "Assault", ply:GetName(), 0 )! [editline]02:36PM[/editline] I did it! It works perfectly now! SetEnemy() was working, but since the NPCs didnt know where that enemy was they couldn't attack. [lua]z:UpdateEnemyMemory( enemy, enemy:GetPos() )[/lua] This tells the NPC where the player is, and as soon as they spawn they hunt that player down!
Look at the Onslaught Evolved gamemode. They basically beeline straight for the players.
Sorry, you need to Log In to post a reply to this thread.