Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity AI Programming Essentials

You're reading from   Unity AI Programming Essentials Use Unity3D, a popular game development ecosystem, to add realistic AI to your games quickly and effortlessly

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781783553556
Length 162 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Curtis Bennett Curtis Bennett
Author Profile Icon Curtis Bennett
Curtis Bennett
Arrow right icon
View More author details
Toc

RAIN AI

Rival Theory's RAIN AI is a very full-featured AI to use and it is free. It includes a behavior tree with similar functionality to React, but has more features built in. In fact, this one won't require any additional scripting to go from point A to point B.

To get this going, we'll need the following bases:

  • A map to move around on
  • A character to move around in the map
  • A route (series of waypoints) for the character to follow
  • A navigation mesh that knows what to avoid
  • An AI to control the character
  • A behavior tree to tell the AI what to do

To start with, you'll need to start a new Unity project, import Unity's Character Controller, and import the RAIN AI package.

Note

Don't get the RAIN AI package that is found in the Asset store. The current release (at the time of writing this book) can be found at the Rival Theory site, rivaltheory.com/rain/.

Perform the following steps:

  1. To create our map, add a plane. Then, add a couple of objects to act as obstacles. It is best if they create a U or V shape to potentially trap the player:
    RAIN AI
  2. Next, drag the predefined character found in Project | Standard Assets | Character Controllers | Sources | Prototype Character | "constructor" into the scene. From the scene depicted in the preceding screenshot, I recommend placing him (the character) on the back left-hand side of the plane.
  3. Next, we need a route. Although there is more than one waypoint system in RAIN, I found that the route will be the fastest for this demo. In the RAIN menu, click on Create Waypoint Route. Name it's game object GreenPath. We will need to call this later, so we want simple, easy names to remember.
  4. In the Inspector panel for GreenPath, click on the Add (Ctrl W) button. This adds a waypoint. In fact, we need to add three. Place the first one on the inside of the V, the second on the tip of the V, and the last on the far edge of the plane, as shown in the following screenshot:
    RAIN AI
  5. Just as in React AI and Unity NavMesh, we need a navigation mesh for this as well. We clearly defined the route, but as you can see, the path is blocked. In the RAIN menu, click on Create Navigation Mesh. Align and stretch it so that it surrounds the area where you want the paths to be determined.

    The Inspector panel for the NavMesh has a property called Ignored Tags. Add Player to this list. (You might need to make sure that the player object actually has that tag selected as well.) Otherwise, the NavMesh will not generate where the player stands, and will prevent its ability to find a path. Then click on Generate Navigation Mesh at the bottom of the Inspector panel. The result should look like this:

    RAIN AI
  6. Next, we need to add an AI to control the character. Select the player object in the Hierarchy panel, and from the RAIN menu, click on Create AI.
  7. Next, select the middle button of the character running in the AI Inspector panel, then click on the Add Existing Animations button at the bottom. This will add all the player's animations: idle, walk, jump, pose, and run. You can refer to the following screenshot:
    RAIN AI
  8. Next, we need to add a behavior tree. Behavior trees are a way to define decisions and actions for AI characters. We will discuss them in more detail in Chapter 3, Behavior Trees. For now, add one by clicking on the head/brain icon in the Inspector panel and then click on the Open Behaviour Editor button. On the right-hand side is a behavior tree drop-down selector, so click on it and choose Create New Behaviour Tree.
  9. Name it FollowGreenRoad. It will already have one element, SEQ (Sequence), under the root BT (behavior tree) node. Sequence it means that it will run any child nodes in order. Right-click on the SEQ node and navigate to Switch To | Parallel, which means that it will run all its child nodes simultaneously.
  10. Let's add the child nodes and then set them up. Right-click on the PAR node, then navigate to Create | Actions | Animations. Right-click on PAR again and navigate to Create | Actions | Choose Patrol Waypoints. Then right-click on the new WAY node and navigate to Create | Actions | Move.
    RAIN AI

    Because the decision is to run things in parallel, it will animate the character and follow the waypoints at the same time.

  11. Click on the green animate node, and set its animation state to walk. It is case sensitive, but you can select which animation the character should use.
  12. Next, select the WAY node. Here, you need to set Waypoint Route to use. This was the navigation route we created earlier with the three waypoints. We called it GreenPath.
  13. For the loop type, we'll make it One Way so that the character only travels to the end and stops there. Also, change the name of the loop to Follow Green Path. This shows up next to the WAY node, and helps explain what is happening.
  14. Finally, set the Move Target Variable to NextWayPoint. This is a variable that we are setting with the next waypoint in the path. When it is reached, the patrol route will set the variable to the next location in the path. We use this in the Move node.
    RAIN AI
  15. Select the move node, and in the properties, set the Move Target to NextWayPoint, the variable that is being set by the patrol route we just configured. And set the Move Speed to a number, such as 3. This is how fast the character will move.
  16. Now that we have created the behavior tree, we need to set the Character AI to use it. Select the AI object under the player object in the Hierarchy panel. On the Mind icon, for the Behavior Tree Asset, set it to FollowGreenRoad. This can be found by navigating to Project | AI | Behavior Trees, or from the selector in the Inspector panel, choose the Assets tab, and it should be right on top.

The demo should be able to run now. The character will move around the block and walk to the last waypoint in the path.

You have been reading a chapter from
Unity AI Programming Essentials
Published in: Dec 2014
Publisher:
ISBN-13: 9781783553556
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime