The Daily Click ::. Downloads ::. Tutorial ::. Object-Avoiding Follower AI

Object-Avoiding Follower AI
Author: DeadmanDines Submitted: 26th November, 2007 Favourites:0
Genre: Tutorial Downloads: 449

Does what it says on the tin.

Two frames, one is a basic example, the second is a complex maze. The little NPC character will follow you no matter where you go.

1st Example

A small square, you walk around the square, and the NPC will follow you. It draws two lines to show the Line of Sight processes that are running.

2nd Example

Maze. The NPC will follow you through the maze. In addition to the features from example 1, you can retrace your steps and 'undo' the path you have laid. This way if the NPC is a long way behind, and you decide you've taken a wrong turning and go somewhere else, the NPC won't imitate your mistake.

This one has better comments.

How it Works

If the NPC can see you, he enters a DIRECT state, where he will just follow your coordinates.

If he cannot see you, he goes into INDIRECT state. The moment his line of sight with you ends, a small marker is placed at the point where the LOS colided with the wall. That marker is then moved away from the wall to give more following space.

From then on, future Line of sight tests are made, not between the player and the NPC, but between the player and the newest Marker.

When these LOS tests fail, another marker is made as before, and the result is a Breadcrumbs-esque engine.

If at any time the NPC can see you again, all Markers are eradicated. They are also destroyed as the NPC 'gobbles them up'.

I found it works surprisingly well.


Only works for one follower.

Review This Download (21 kb )

Posted by Knudde (Shab) 26th November, 2007
Rated :

Awesome Dines! You are so friggin helpful!
Posted by Assault Andy 27th November, 2007
Rated :

This is a very neat engine! Do you think you will ever develop an engine for use with multiple followers?
Posted by ~Zigzag~ 27th November, 2007
Rated :

Can it be implemented into platform style? 'Coz that'd be really useful!
Posted by DeadmanDines 27th November, 2007

Lol, thanks all

Multiple Followers
I dunno how that would work, there are various ways it could be tackled. You could possibly create an invisible object which acts exactly as the follower does now, and have all the followers follow THAT.

Another alternative could be to give the followers an ID, so each follower follows the next follower (so NPC5 follows NPC4, who follows NPC3, ... to NPC1 who follows the Markers). That'd result in your buddies going single file, but only when they can't see you. They'd all automatically follow YOU when their state is DIRECT.


I think this exact system wouldn't be doable for Platforms. I've been trying to work on an alternative, there are two basic ways of tackling it:

1. Give your AI characters the EXACT same movement as you, so they walk and jump with the same variables. Then when you jump, leave a marker. The newest marker will always record your keystrokes in an Alterable String, like this:


0 = stopped, 1 = left pressed, 2 = right pressed

Then when the follower hits this Marker, he simulates those key presses himself. The result is a jump EXACTLY like yours. This has the advantage that if you do a highly complex jump, from ledge to ledge, the AI will imitate you exactly.

The downer is that the AI will also jump when he doesn't need to (if you just jump for the hell of it, he'll copy you a moment later, and it looks dumb).

2. Get the follower to scan his surroundings, based on where you are. Is there a wall between you and he? Is it low enough to jump? Is there a drop between you and he? Is it close enough to jump?

This would be the more difficult of the two, but they both have pros and cons.
Posted by DeadmanDines 27th November, 2007

Oops, just a thought on Platforms, point 1:

When you leave a jump marker, you could set a 'Provisional' flag. When the player lands AFTER that jump, the Marker is tagged as 'Finalised', and can now be imitated by the NPC.

This way, when you land, a quick test could be run to decide whether the jump was unnecessary. If so, the Marker would not be finalised, but deleted.
Posted by Torava 27th November, 2007
Rated :

Damn. It's not possible that buddies follow player. But awesome engine anyway.
Posted by Torava 27th November, 2007
Rated :

Oh, you already told about multiple followers.
Posted by sysoptic 28th November, 2007
Rated :

Posted by ..:.Phox.:.. 29th November, 2007
Rated :

awesomely done Dines great work
Posted by ~Zigzag~ 30th November, 2007
Rated :

Well, thanks for the tip. I've never been able to get followers to work properly.
Posted by Xhunterko 11th March, 2008

I've thought about jump markers too. Perhaps for platformers, try leaving everything the way it is, and jump markers, yet take out the automatic Y axis. And only in act the Y when the follower collides with the jump marker.
Posted by Knockturnal 7th December, 2008

Broooooooken link





Worth A Click