Creating a Shooting Game (recap)

Making use of basic Ray Casting and Nav Mesh to creating an shooting game.

Simon Truong
7 min readMay 31, 2023

--

In this recap we are going to summarized everything that we have learned with working Nav Mesh and Nav Mesh Agent, so we can create an shooting game which the objective is to shoot moving targets on the screen in while the enemies are mid movement.

Since we are provided with the proper setup with the environment and player controller, what we need to focus on is the programing of the AI spawn, movement and the ability to be targeted by the Ray Cast.

Setting Up the Waypoints:

In the provided scene we are going to first setup the waypoints which the future enemy game object can travel between. We are going to add in at least 4 to 6 different waypoints so we can have the enemy stop at least once to allow variation within our gameplay.

Most Importantly is to setup both an Starting and Ending waypoint.

It will be nice to have an overall empty game object to house all the waypoints together to keep things organized.

Adding Navigation:

Before we can continue with the Enemy setup we first need to bake in the Nav Mesh platforms. To do this, we will need to open up the Navigation tab which can be reached by going to Window > AI > Navigation.

Next we are going to select the platforms which we will want the AI Enemies to be able to walk on so we can Bake the pathway into the Nav Mesh. Once you have selected them all, within the navigation tab we are going to hit the “Bake” tab so we can click the back button on the bottom right.

Once the baking as been completed you will have an scene like this:

The blue pathway is the only accessible path which the AI is able to walk along.

Setting Up the Enemy:

When setting up the Enemy, we need to setup the enemy game object, and before we can start animating or getting the 3D model of the enemy, we need the basic prototype type of the enemy first. We can start with enemy movement from one waypoint to another. I started out with a basic capsule as the enemy prototype.

Within the Inspector I added an Nav Mesh Agent so Unity will know that this is an game object that will be moving along the preset path.

Scripting the Movement:

To make the capsule move we need to create an new C# script and name it Enemy Movement. Within this script we are going to use an Enum to control what current state the enemy can be in. As of right now we will need a total of 3 different states: Walking, Hiding and Death. (With Enum you are able to expand this further down the progression line if you so desire)

Next we are going to identify some local variables which will add functionality to the enemy game object. We will need an private int to identify the current Destination, the Enum state itself and the Nav Mesh Agent that is attached to the enemy.

Next we are going to add an private local variable called “waypoints” and this will help the Enemy AI to gather the current array of preset waypoints.

This private Transform “Waypoints” is going to be empty for now because this variable is actually going to be set within the Game Manager script (that isn’t created yet).

In the Update Method we are going to create an “Switch Statement” to control the swapping between different Ai States (aka Walking, Hiding and Death)

Don’t worry about the missing methods as we will slowly create them.

To start, we will focus on the movement method which we called “Calculate Movement”.
In this method we are first going to set the Nav Mesh Agent “is Stopped” to false just as a safety percussions to make sure that the Enemy will always be moving within it is in the Walking State.

Next we are going to create an IF statement and calculate the Nav Mesh Agent’s remaining Distance from the current waypoint, if that distance is less than 0.5f, we will set it so that the agent position will equal to the current waypoint.
In another IF statement we are going to calculate if the current Destination is equal to the overall waypoint length then we are going to Trigger a Hiding method ELSE we will add ONE to the current Destination.

This basically makes the Enemy AI to hide within there is contact with an waypoint and then add one to the current waypoint so the next target to move to will be set.

Just by using this script alone, we should be able to make the capsule move from one waypoint to another. If there is an error, we are going to solve it within the next topic, the Game Manager.

Setting Up the Game Manager:

If there is an error when testing out the enemy movement due to missing an waypoint variable, it is because we haven’t created one yet. Since the Enemy AI is going to be spawned into the game during game play, we are going to need the waypoint array outside the Enemy Movement script, therefore the most practical way is to place the transform for the waypoints into a brand new Game Manager script.

Since the Game Manager is of an Manager class, it is best make this script as an Instance based script.

Next we are going to create an serialized field for the private waypoint array.

Since this value is within the Game Manager script and we need to pass the Transform data to the Enemy script we need to make a public Transform method and return the data “waypoints”.

Back into the Enemy Movement script in the Start method, we are going to gain access to the public method like so.

Once that is completed you will see that the Transform positions will be transferred to the newly spawned Enemy Ai upon game play.

Scripting the Hiding State:

Now that the movement section has been completed and working, we will need to input the hiding function. Since we want the AI to hide behind certain waypoints at random seconds and then return back to the Walking State we need to utilize coroutines.

Start by creating a new method called “Calculate Hiding” so we can keep the same naming scheme as the Movement method.

Within this method we are going to create an localized variable called “Hiding Seconds”. This variable will control the random seconds that will allow the AI to hide behind obstacles. We will set and Random Range between 3 seconds to 5 seconds as a testing phase (we can always alter it in the future whenever we need to).

Next we will need to create another local bool called “is Hiding”. This will be the Trigger to allow the AI to stop and hide.

Finally we will need to create the IEnumerator that will time everything. Within this numerator we want the Nav Mesh Agent to stop what it is currently doing and pause.

The animation bool and float are when you start adding a 3D model to replace the capsule later on. Ignore this as we will go over that in the future.

Then we are going to do a yield return new wait for seconds and pass in the local float value of “Hiding seconds”. Once that is done, we will need to reverse everything that we have applied and set it back to normal. This also means that the current Ai State is going to return back to being “Walking”.

Finally we are going to setup and Trigger method which will toggle the hiding function altogether. In this method we will set the is Hiding bool to true which will toggle the Calculate Hiding method and also at the same time swap the current Ai State to “Hiding”.

To toggle this trigger we will need to go back into the movement method to finish the IF statement.

In the next article we will finish up the Enemy AI and also work with the other elements of the game to make it complete.

--

--

Simon Truong
Simon Truong

Written by Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.