Game Development Reference
In-Depth Information
5. Next, we'll add an enemy in the same way, with Capsule . The enemy needs
a component called Nav Mesh Agent , which is a component capable of us-
ing a NavMesh to move around, so add it. Now, the game object has the abil-
ity to walk around, but it has nowhere to go. To get it moving, we need to add
the enemy AI agent.
6. Next, we get to the AI for the enemy agent. In React, a behavior tree is called
a Reactable . To add a reactor, we start the Project explorer, in a folder of
our choice, by navigating to Create | Reactable .
7. Once created, rename it to EnemyMovement . In the Inspector, it has a list of
behaviors for it. We'll need to add a script, which can be found in the topic's
contents: \Scripts\React AI\FollowThePlayer.cs .Without goingin-
depth in the code, let me explain the following key points:
• The C# file was copied from a sample script provided with React AI
that made a character move away from a target.
• It was rebuilt to make the player the target destination, and also to
turn seeking on and off by using a button. It is not hard to adapt these
scripts.
• Unlike normal mono behaviors, you use a special Go method. The go
method is called by React AI only if it is selected to be used.
• In the Start method, we see it obtain the NavMeshAgent that we at-
tached to the enemy in the Inspector panel.
• In the Go method, we see it feeding the destination to the NavMeshA-
gent, and then checking to see whether it has already found a path.
Once it does, it just goes.
• All uses of that agent are still following standard Unity calls to use
NavMesh, and can be applied without using the AI, by placing this
code in a traditional behavior Update method.
This script needs to be added to the Inspector for the EnemyMovement as-
set, and also to the Enemy game object.
8. Once the script is attached to the enemy, the Inspector will reveal that it has
a target. Drag the player from the Hierarchy panel into the player attribute
on the Inspector panel.
9. Finally, we have the behavior tree to set up. In the Project panel, right-click
on the EnemyMovement asset, and click on Edit Reactable . A behavior
tree pops up an editor, which is how we train our AI.
Search WWH ::




Custom Search