How to set up way-points in unity

A waypoint is a reference point used for navigation purposes by in-game characters, most commonly used in strategy games and squad based games. They are also commonly used in game design for defining AI paths in nearly every genre. They are a simple concept and an extremely powerful tool.

Today, I’ll go over the ins and outs of setting up a waypoint system in unity.

First, create an empty GameObject. This will need to be saved as a prefab (by dragging it from the hierarchy to the project panel). If you want the player to see it, then create a sphere as a child for it, if not then we can create a gizmo sphere, that will show up in the editor only using:

 Gizmos.color = Color.yellow;
 Gizmos.DrawSphere(transform.position, 1);

This will draw a yellow sphere with 1 unit radius on the transforms’ position, which is really easy to see in the editor, and is great for placing static waypoints for things like enemy AI, or showing invisible areas (this is very useful if you ever roll your own physics)

Now, one waypoint is not usually very useful, and you are probably going to need a list of these, that we move towards and that get deleted once they are reached. A List is what you need. You can fill the list manually in editor or add to it dynamically in-game using List.Add(), up to you. You will need to set your entity to move towards the waypoint that is kept at the start of the list (0), and when it reaches that waypoint, set a local variable to store the waypoint game object, remove that waypoint from the list (List.RemoveAt(0)), and then delete the local variable. this avoids the list from containing a null reference, in case something else tries to access it before you clean up the list.

When you remove a waypoint like this, using RemoveAt(), it will shift all of the items in the list down to fill the empty slot. This means that if your entity is always trying to move towards the first entry, it will now move towards the next waypoint that was in its list, without having to iterate through the list, or needing an int to keep track of its current waypoint, as it will always just move to the first available in the list. Just remember to surround the logic with a check to see if the list has any entries at all. you can achieve this with “if (List.Count() >0)”.

I used this myself in a game I recently made that uses the unity navmesh agent for movement, which just needs a vector3 for its location to head towards, which I will go over in my next blog.

How to make a finite state machine

A finite-state machine (FSM) is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A particular FSM is defined by a list of its states, and the triggering condition for each transition. -Wikipedia

if your control scripts are a hot mess of nested if statements and bool toggles, then an FSM is for you.

A state machine works by having a bunch of different states that it can transition to. each of the states can have different behaviours. An example of this is an automatic door. While it is closed, it can either stay closed or open, but while it is open it can only stay open or close. Another example is in a pet game. It may have an ‘energy stat’ that is constantly having an amount taken away from it every second. While the pets’ FSM is in the ‘idle’ state, this number could be 1. While it is playing, this number could be 5 (as it is exerting energy) and while it is sleeping, the number could be -2 (regaining energy). When your entities behavior starts to get more and more complex, the use of one or several FSMs becomes more and more necessary. They are also very useful for menu’s, and if you use Unity, you may have seen one already (Mechanim, Unity’s built-in animation control system)

So let’s draw out a simple state machine for an automatic door. Doing this is good practice, as a larger state machine will very quickly grow to a size larger than your head can contain.

Draw out your state machine on paper. Start by drawing each state that the machine’s entity can be in. For a door, this would be Open, Opening, Closed and Closing. Then write down the Inputs that the machine will receive. For my example, this would be from the sensor, and would be ‘Detected movement’ and ‘Lost movement’ (or simply signal on/off). There is also Input from the door itself, once it has completed the task of opening and closing, which we will name ‘Completed’. Now for each state, we draw transition lines to other states, based on those inputs. If the door is Open, and we get the ‘Lost movement’ input, the door should close, so we transition to Closing. once we are in Closing, and the task of closing has finished, we get the Completed input, so we make a transition line from Closing to Closed, with the Completed input. If we are in the Closed state, and we get theDOORfsm ‘Detected movement’ input, the opposite happens. If we are in the Closing state and receive the ‘Detected movement’ Input, we can transition directly to the Opening state, and vice versa. Here is what it should look like by now:

Once you have that down, you need to make up the framework for an FSM, or download one someone else has already prepared. Once you have that, you can set up all the states and transitions in the same way that you drew out on paper.

This is a framework for an FSM for use in unity, with an example on how to use it.

Here is a step by step guide on creating a state machine by Jesse Warden