In this tutorial we’ll set up a simple move AI to let enemies hunt the player.
This tutorial continues the sub-series for setting up battles.
Move AIs are used to let combatants determine targets for movement, while the actual movement is handled by the combatant’s movement component setup. Learn more about move AIs in this documentation.
In the next tutorial we’ll spawn our enemies in the field and start battles when getting close to them – for this, we want them to hunt the player, so we’ll set up a simple hunting move AI.
Idle Schematic #
First, we’ll set up a simple schematic that’ll be used to animate a combatant’s idle behaviour between two waypoints. We’ll just use a random wait time, though.
Wait #
Add Node > Base > Wait
We’ll wait for a random time between 1 and 5 seconds.
- Random
Enable this setting. - Time
Select Value > Value.
Set the value to 1. - Time 2
Select Value > Value.
Set the value to 5.
And that’s it for the schematic. Click on Save Schematic to save it, e.g. as RandomPatrolWait.
Move AI Setup #
Navigate to Combatants > Move AI, we’ll change the Default move AI.
Base Settings #
- Name
Set to Hunt.
Move Range #
We’ll limit the range a combatant will move in – this is measured from the combatant’s spawn position.
- Use Move Range
Enable this setting. - Range
Select Value > Value.
Set the value to 10.
Auto Stop #
- Use Auto Stop
Disable this setting.
We’ll not use this, but it can be useful, depending on the movement component you’re using for your combatants.
Speed Settings #
These settings define at which speed a combatant moves for different modes. The default settings are enough, we’ll just change the Waypoint Speed.
- Speed Type (Waypoint Speed)
Select Walk.
This’ll use the combatant’s walk speed.
The Speed setting is only used as a fallback if no walk speed could be determined.
Waypoint Settings #
These settings handle waypoint movement, either for randomly selected positions or fixed waypoints defined by a Combatant Spawner or Add Combatant component used to add the combatant to the scene.
- No Combatant Radius
Enable this setting. - Enable Random Patrol
Enable this setting.
We’ll randomly patrol within a defined area. - Patrol Radius
Set to 10.
Idle Settings #
- Enable Idle
Enable this setting.
Click on Add Idle Schematic.
- Schematic Asset
Select the RandomPatrolWait schematic we just created.
Enemy Detection #
These settings handle how the combatant will detect enemies. We’ll detect enemies on sight within 10 world units and within an angle of 90 degrees of the forward direction of the combatant
- Use Enemy Detection
Enable this setting. - Timeout
Set to 0.5. - Range
Select Value > Value.
Set the value to 10.
Click on Add Detection.
- Type
Select Sight. - Use Raycast
Enable this setting.
We’ll do a line of sight check. - Layer Mask
Select the Default layer (only).
To first unselect all other layers, select the Nothing layer, followed by the Default layer. - Use Child (User)
Select Path.
We’ll define the path to a child object. - Find Child (User)
Set to Head. - Use Child (Target)
Select Path. - Find Child (Target)
Set to Head. - Angle
Set to 90.
Target Position Check #
- Lost Position Check
Enable this setting.
When the detected target leaves the detection range, the combatant will keep following it until the lost interval runs out.
Otherwise the combatant will move to the last known position.
The target checks define how often the target’s new position is forwarded to the movement component – depending on the used component (we’ll use NavMesh) this can be more or less performance heavy. We’ll add different checks do increase the update interval when the target gets closer.
- Interval
Set to 0. - Minimum Distance
Set to 0.
Click on Add Target Check.
- Interval
Set to 0.2. - Minimum Distance
Set to 2.
Click on Add Target Check.
- Interval
Set to 0.5. - Minimum Distance
Set to 5.
Click on Add Target Check.
- Interval
Set to 1. - Minimum Distance
Set to 10.
Hunting Settings #
We don’t have to do more than just enable hunting.
- Use Hunting
Enable this setting.
Combatants General Settings #
Now we’ll set up the default movement component (which will handle the actual movement) and default move AI used by our combatants. The combatant directly controlled by the player will never use a move AI – in case you spawn other members of the player group, they can use a move AI, e.g. to follow the player around.
Navigate to Combatants > Combatants > General Settings.
Default Movement Component #
The scenes are set up for NavMesh, so that’s what we’re using to move the combatants.
- Component Type
Select Nav Mesh Agent. - Add Component
Enable this setting.
Default Move AI #
- Use Move AI
Enable this setting. - Move AI
Select Hunt.
And that’s it for the editor setup – don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.
Radius for Combatants #
The move AI’s range checks (and other range checks) can also take the radius of a combatant into account. You can set up a radius by adding a Radius component to the combatant prefabs – and that’s what we’ll do.
The setup is pretty simple, just add a Radius component to all combatant prefabs found in Assets/Tutorial Assets/Prefabs/Combatants/Enemies/ and Assets/Tutorial Assets/Prefabs/Combatants/Player/.
The Radius component will automatically set the radius based on the game object’s collider (Character Controller in case of our combatant prefabs) when added.
You can select multipe prefabs and add the combatant to all of them at once.
And that’s it for the move AI (and radius) setup!
Next, we’ll look into spawning combatants in our scenes.