The video posted above shows off a simple chasing game where the player will be pursued by a seeker who has obstacle avoidance so that they can move around obstacles. The seeker will progressively get faster as time goes on so eventually it will catch the player. Additionally there is a counter in the top right corner of the screen that shows how many collisions the seeker makes throughout the game. Every time the seeker makes contact with an obstacle one is added to the counter.

The obstacle avoidance algorithm is meant to help agents avoid objects in the environment. Unity has a few tools that were useful in the development of this algorithm so I will talk about them as well.

The first way I went about tackling obstacle avoidance was using Raycasts. A Raycast is method where you project a line in a specific direction. If the line anything collides with anything you can get that information. For obstacle avoidance it acts as a feeler or whisker helping the avoiding agent detect what it needs to avoid. Unity has many different types of Raycasts such as the Line Raycast, CircleCast, BoxCast, CapsuleCast and many more. Raycasts such as CircleCast projects a shape (a circle in this circumstance) along the line you would get make with a normal line raycast.

To create the obstacle avoidance in my video I split the algorithm into a directional component and a speed application component. Most of the obstacle avoidance algorithm happens in the directional component as where the calculations for the agent’s direction is done. To help determine the direction I use a CircleCast from the Physics2D class. If an obstacle collides with the CircleCast I take that obstacle and figure out where it is relative to the agent. To do that I get the obstacle position in the agent’s local space and substract the agent’s position from the obstacle’s position. Once I get the difference I look at the x value. If the x value is negative then the obstacle is on the left and the agent must start turning right. The opposite is true if the x value is positive.

After any obstacles are detected any needed needed changes are added to an initial the direction provided by the seek steering algorithm. Once the directional calculations are done the directional vector then has the agent’s speed applied to it and the agent’s Rigibody2D’s velocity component is set equal to the result.