Predictive Chase AI for Unity

Many years ago now, I designed a game on paper for iPhones. I never built it. Never new how.

However, now that I have gotten familiar with Unity I have been able to write parts of this game, as time permits. And as you can see from reading below my time is spread out pretty thin among my many interests.

That said, I managed to make a very playable as-yet-incomplete Hover Taxi game that I will write much more about in future.

But for now, back to my Rolly Ball game. Based on a game I wrote in BASIC on the Commodore 64, which was based not-so-loosely on Q*Bert, but with a level editor, I decided to make a game where you control a rolling ball with accelerometer. I haven’t quite cracked that part yet, so for now I’m using an XBox 360 controller and it works fine.

So far I have the ball rolling around changing the color of grid tiles. That’s the main game.

But there has to be more. Much more.

Enemies

Enemies start out pretty docile, just meandering around the board undoing your work, repainting squares back to their original colors. You have to stop them by hopefully pushing them hard enough to break them, or push them off the board if I allow that. (A no-edge board presents major problems for a player.)

Later, enemies begin to chase, and eventually get quite aggressive in not only chasing you but predicting where you are going and getting there first, and even adding a sudden pulse to push you suddenly in a different direction.

How Do I Make An Enemy Hunt?

I started a totally new Unity project to explore this AI idea:

The Player is a spherical rigidbody. That’s a physics object that can roll around and obey the game’s physics laws. To make it move, I use an analog joystick pad to add impulse in the direction the joystick is pointing, at the force the analog joystick is pointing. (ie: you can nudge the stick and get a small push, or jam it all the way for a larger push.)

So that’s been done for months.

But what about an enemy ball? How do I get it to be a threat?

Just telling the enemy ball to try to get to where you are at this very moment is not so great. By the time it gets there, you’re long gone.

So I set up a scene in which an indicator circle is placed at the Player’s velocity vector. That is, an X, Y, Z coordinate which indicates where the ball is going at any given Physics Frame.

If I set the enemy up to try to get there, by adding an impulse in that direction, that will be better. It will get to where you’re going. However, you’re not just rolling in a straight line.

You have a joystick to tell your Player ball you now want to head off in a different direction entirely, and because physics has momentum, you don’t suddenly go in that direction, but you angle towards it naturally.

So now I add another vector of the Joystick’s intended direction, and I add that vector to the position of the ball’s velocity position.

To visualize this, I created two indicators.

One is placed, every frame, at the position the ball’s velocity wants it to go.

The other is placed relative to that object, in the position of the joystick’s vector.

Each of these values is multiplied by an intensity variable, so I can tweak how much to tell the Enemy ball the player wants to get there.

I used two line renderers to draw lines between the objects so you can easily tell what the motion vectors are.

Here’s a screenshot to tide you over until I fill in the above with better shots when I get time.

Leave a Reply

Your email address will not be published. Required fields are marked *