Reader small image

You're reading from  Unity AI Programming Essentials

Product typeBook
Published inDec 2014
Publisher
ISBN-139781783553556
Edition1st Edition
Tools
Right arrow
Author (1)
Curtis Bennett
Curtis Bennett
author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett

Right arrow

Chapter 9. Driving

In this chapter, we will look at another specialized AI, driving. The other AI we have looked at so far had pretty simple movement for characters. However, car movement needs to take into account physics, and this makes driving AI more complex, which is why we need an AI system specially designed for driving. The AI driving system we will use for our demos is Smart Car AI. Smart Car uses Unity's built-in navigation mesh system, so we will also take a look at it.

In this chapter, you will learn about:

  • Setting up the AI driving system

  • Creating a Unity navigation mesh

  • Using Smart Car to drive AI along a path

  • Using Smart Car to drive and avoid obstacles

An overview of driving


When designing AI for our characters, one of the basic concepts is to have AI move with the same rules as the player. If you ever played any old racing games, sometimes the opponent cars wouldn't follow the same physics as the player, zooming along unrealistically and therefore creating a bad player experience. So, it's important to take car physics into account, including the shape of the car and four wheels, and have the AI move in the same way as the player. This is the main reason for using an AI system especially designed for autos and driving, instead of a general-purpose game AI system we have been using such as RAIN.

The driving system we'll use is Smart Car AI by BoneBreaker, which at the time of writing this book is available in Unity Asset Store for $10. It takes into account physics for the car and uses ray casting to sense the car's environment. It actually uses two systems for navigation, which are Unity's built-in navigation system to determine paths...

Setting up a Smart Car vehicle


As Smart Car uses a realistic car setup, there are many options to configure your vehicle. To create a Smart Car vehicle, you'll need a car model with different models for wheels and Unity wheel colliders setup on them. After adding a car model to your scene, import the Smart Car 2.3 package and attach the SmartAICar2_3.cs script from SmartAICar2.3/Scripts. In the following screenshot, you can see some of the Smart Car AI fields from the script:

There are fields here you can customize such as Engine properties, including Engine Torque Curve, and the distances for the ray casts used for sensing. Most of these can be left to default values, but to run the script, you'll need to fill in the wheel properties, dragging from your model the colliders for the four wheels to the Wheel_FL, Wheel_FR, Wheel_RL, and Wheel_RR properties. You also need transforms for the wheel set. Also, there needs to be a transform for center of mass (COM), a lower point in the middle of...

The Smart Car AI demo


Now, we'll start setting up our driving demo that will have a car driving along a road and avoiding obstacles.

Setting up a Unity test scene

Besides needing Smart Car, we'll need an environment for our AI cars to drive in. We'll use Car Tutorial v1.3 that is made by Unity, which you can download for free from the Asset Store. Import the project and open TheTrack scene from the imported Scenes folder. Next, add a car to the scene. The car prefab that comes with Car Tutorial doesn't have the complete wheel physics setup, so you can configure it using the steps in the last section or use the EnemyAICar prefab from Smart Car. To make the car work better with the Tutorial scene, extend the rays a little, set Wide and Tight Ray Distance both to 40 and Long Ray Distance to 50. This keeps the car from hitting obstacles when going too fast and missing tight turns. Once you have a car in the scene configured for Smart Car, select the Main_Camera object and set your car to Target...

Additional features


We've just completed creating a driving demo with a car avoiding obstacles, but there are a few more things you can do with driving AI. We can add brake and drift zones to help configure the general behavior of the car as it drives around the scene, and we can integrate Smart Car with other AI systems such as RAIN.

Adding brake zones and drift zones

Another interesting thing you can do with Smart Car is define zones in the level to either cause the car to brake and slow down or adjust the friction of the car to make it drift. These are similar to the vector fields we saw in Chapter 5, Crowd Control, where we place them in the level to affect the AI, and they aren't visible to the player but are good to use for scripting level experiences. To create a brake or drift zone in your game, add a cube to the game (go to GameObject | Create Other | Cube) and scale and translate the area you want to tag in the level. In the Inspector window for the cube, set its tag to BrakeZone...

Summary


In this chapter, we looked at Smart Car, an AI system specifically for car AI. We discussed why automotive AI is different than most AIs because of the physics involved, and we also saw how to set up a car model, create a path for the car, and add obstacles. We also looked at using Unity's built-in navigation mesh system, instead of using third-party ones such as RAIN, and discussed additional features for car AI and how we can integrate it with another AI system such as RAIN.

In the next two chapters, we will look at how to combine character animations and AI to give them a realistic appearance and learn more about creating complex navigation meshes for different AIs.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity AI Programming Essentials
Published in: Dec 2014Publisher: 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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett