AI, Pathfinding, and Events
This tutorial covers setting up AI, pathfinding and events. We we learn how to use AI scripts and the navigation mesh generator to create an exiting FPS scene.
Playing the Game
We start out with the FPS character controller. You will notice that this time we have a weapon loaded in front of us.
Shoot the weapon by pressing the left mouse button. We can reload the weapon by pressing the 'R' key on your keyboard.
The flashlight is attached to the player and can be toggled on and off with the 'F' key.
Press E to interact with control panel which is located on the upper walkway in the wide hall.
Music starts playing and soon enemies will appear. These enemies will attack you.  After killing the enemies, go down to the stairs and you will notice that the elevator has appeared. We can go on to this elevator, press the button on the control panel and use the elevator to continue the level.
Flowgraph Editor
Open up the flowgraph editor. You can see that there are 3 control panels. If we select the control panel in the scene, it becomes highlighted in the flowgraph editor. The first control panel controls the elevator, sliding doors, activates music and the enemies.
Select control panel if you haven't already done so. You can see that the ' PushButton' script is attached. Here we can specify which sond plays when the button is being used. We can also say whether it is enabled. If the enabled button is not enabled, interaction is not enabled.
Once the control panel button is pressed in the scene, it calls the 'use' output. This calls the trigger Delay. This is a timer which counts down till it calls the output. That output on its term start playing music.
Pivots
Lets locate the music delay trigger in the scene. You will see a small icon in the scene. This represents a pivot. A pivot is a 3d point in your scene and nothing more. It doesn't render anything and it doesn't have physics. The pivot has the triggerDelay script attached. delay is set to 2.5 seconds. The second pivot has the noise.lua script attached. This is actual sound that plays.
Doors
The 'Use output' also calls the Monsterdoor. It will open the door for us. The monster door is located halfway through the scene and prevents the player to see the monsters hiding behind it.
Elevator
The elevator also uses the sliding door script. However, instead of going sideways it now goes up and down. The first control panel connects to the 'Open' input.
Elevator Controls
The other 2 control panels also interact with the elevator. The control panel on the elevator itself (see image below) toggles the elevators state.
The control panel at the bottom of the elevator shaft has a connection to the 'Close' input.
AI
Now lets have a look how the AI of the enemies has been set up. Select one of the monsters behind the locked door in your scene. You will the 'MonsterAI' script attached. when a monster is in range it will attack the player. Since it is not enabled it will not do anything.
Let's go back to the flowgraph editor. We can see that when we press the button on the control panel it will activate the monster.Once the monster has been activated it will move to the AI navpoint that we specified in the script. This navpoint is located at the bottom of the stairs in the wide hall. When the monster has reached the wide hall, it will spot the player and it will set its target to the player automatically.
Weapons
How is the weapon set up? Locate in the scene tab. It has the FPS player script attached. We can say whether the flashlight works. We can attach a weapon prefab via the script so that the player starts the level with a weapon.
We can also set the 'Team' of where our player belongs to. If we set it to 'Good' the enemy will not attack us. If we set it to 'Bad', the enemy will not attack us.
Navmesh Generation
Leadwerks has the build in capacity to generate aq navigation mesh. In order to do so, go to Tools -> build Navmesh.
A small menu appears with 2 options. The most important one is the 'Max edge length'. This determines how detailed your navmesh should be. The lower this number the longer it takes to generate.
After you are done building the navmesh, it is automatically shown. The blue areas represent where NPC's can walk.
You can toggle the view of the navmesh at any given time by going to View - Show navigation.
Monster Door
Look at the door where the monsters hide behind. You can see that the navmesh goes through it. (as intended).
If you don't want this to happen then go the physics tab of the selected object. Here you can see a checkbox that says 'Navmesh obstacle'. If we check it and rebuild the navmesh we see it does not generate a path through door.
Conclusion
You now know how to build a first-person shooter game with many interesting game mechanics. Can you think of other ways these features can be used to make new levels?