Avoiding obstacles

In this section, we will explore obstacle avoidance. As a first step, we need, of course, obstacles. We will set up a scene similar to the one shown in the following screenshot. Then, we will create a script for our character so that it can avoid the obstacles while trying to reach the target point. The algorithm presented here uses the raycasting method, which is very straightforward, but on the other hand, it can only avoid the obstacles that are blocking the path directly in front of it:

A sample scene setup

To create this, we make a few cube entities and group them under an empty game object called Obstacles. We will ...

Get Unity Artificial Intelligence Programming - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.