Entities 220: Lesson 5
NPC Spawning and NPC Routing

In most SP levels, you will want to have lots of NPCs for the player to fight.  The problem is that you don't want every NPC to be in the level when the game starts - that would be horribly inefficient, since the engine would need to calculate AI information for every single one of them until you kill them.

Instead, you need to trigger NPCs to spawn as a person progresses through the level.  For example, if there is a section where only one door leads to the next area, the same thing that triggers that door should trigger the NPCs in the next section to spawn.  You should accomplish this with use of the trigger_once.

For our example, simply place a large trigger_once brush inside the building like you would any other trigger.  Then place an NPC on top of the building.  Target the trigger_once at the NPC.

When you enter the game, there will be no enemy near you... but when you enter the building and come back out, suddenly there will be!  You can also use this technique to spawn enemies behind the player after they have progressed to a certain point.

Be creative when you place NPCs!  There's nothing worse that a bunch of NPCs just appearing in a big group and standing there.  It's much better to send them in groups of three or four, from different directions.  Make your player think!

It is also vital that you create a way for NPCs (note: not talking about MP bots) to move around.  Without NPC routing, your enemies will just stand there.  While that might be fine for snipers, you want your NPCs to move around!

The process is surprisingly simple, but you must understand NPC routing.  Any NPC will run to the closest point_combat in his line of sight.  In lieu of seeing a point_combat, an NPC will look for the nearest waypoint on its own route to run to in the process of getting to a point_combat.  If there is no point_combat at the end of its series of waypoints or if it can't see the next waypoint, it will not even try to find it.

To set up this system, add a point_combat entity where you want the enemy to stand when he's done fighting.  Then make a series of waypoints for the NPC to follow to get to its point combat.  Then target the NPC at its closest waypoint.  Target that waypoint at the next closest waypoint, and so on, down the line, until you get to the point_combat.

In our example map, I made a short wall with two Stormtroopers behind it on separate paths:

Thus, when you play in the game, the Stormtroopers will just look around until they see you, at which point they will run along their waypoints until they reach their point_combat - at which point they will stand there until you kill them.

Do note that all lightsaber-wielding NPCs ignore any pathing you give them and will instead run their own AI scripts.

If you want more complex behavior from NPCs, you'll need to learn scripting.

Back to Home