A simple movement AI moving to random waypoints.
In this tutorial we’ll create a schematic to move a game object to random waypoints using NavMesh. When reaching a waypoint, the game object will optionally perform an idle behaviour (using a tagged machine, e.g. wait for some time) and select a new random waypoint.
You can download the finished schematics here.
First we’ll create the schematic handling the random waypoint and movement. It will check if the destination was reached, start the tagged machine with the idle behaviour and move to the next random waypoint.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
Add Node > Movement > NavMesh > Is On NavMesh
This node checks if a NavMesh agent is on a NavMesh and active. If that isn’t the case, it would be useless telling the agent where to move and would result in warning messages.
Add Node > Movement > NavMesh > NavMesh Remaining Distance
We’ll use this node to check if we’ve reached the destination (i.e. we’ll check if we’re close to it).
This node is connected to the Success slot of the Is On NavMesh node.
Add Node > Machine > Start Tagged Machine
We’ll use this node to start the optional idle behaviour through a tagged machine.
This node is connected to the Success slot of the NavMesh Remaining Distance node.
Click on Add Tag to add a starting tag.
Add Node > Value > Vector > Vector3 Multiply
We’ll use this node to create a random position within the random movement radius of the game object (stored in the local start variable radius).
Add Node > Value > Vector > Vector3 Transform Value
We’ll use this node to transform the random position we’ve created into world space. The random position we’ve created is around on X=0, Y=0, Z=0, so we need to translate it to the moving object.
Add Node > Movement > NavMesh > Store Sample Position
We’ll use this node to find the closest NavMesh position to our random waypoint.
Add Node > Movement > NavMesh > NavMesh Destination
Finally, we’ll set the destination for the NavMesh agent to the new random waypoint.
That’s it for the random waypoint schematic – click on Save Schematic and save it e.g. as RandomWaypoint.
Now, we’ll create a schematic for a simple idle behaviour – waiting for some time.
Create a new schematic and change the following settings.
Add Node > Base > Wait
We’ll use this node to wait for some time. The wait time will be defined by the local start variable time.
And that’s it for the schematic – click on Save Schematic and save it e.g. as IdleWait.
The random waypoint schematic will be added using a tick machine. The game object you want to move also needs a NavMeshAgent component.
Add a tick machine component to the game object (e.g. using the component menu: Makinom > Machines > Tick Machine) and change the following settings.
Click on Add Variable to add a start variable.
We’ll add the idle behaviour using a tagged machine, started by the tag idle. By using a tagged machine for that, you can add different idle behaviour to different game objects, e.g. playing an animation or rotationt the game object.
Add a tagged machine component to the game object (e.g. using the component menu: Makinom > Machines > Tagged Machine) and change the following settings.
Click on Add Starting Tag to add a tag that can start this machine.
And that’s it!
Need to stop your random waypoint AI at some point? E.g. when the game object should chase the player instead.
Use a bool object variable to do that – use it as a variable condition in the tick machine’s Condition Settings to prevent the machine from starting.
© 2015 Gaming is Love e.U.
Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!