Game Development Community

Basic AI - Unit follow player

by Phillip O'Shea · in Torque Game Builder · 02/10/2008 (6:34 pm) · 3 replies

I'm having trouble getting units to follow the player nicely.

Basically, I have set up my scene so that it is broken into one large grid. As an object moves around the grid, it tells the scene that the cells he is on are active. This is so that my units can move to the player, but not get too close to him.

When a unit is told to follow the player, it will check to see the players location and then head in that direction until he cannot get any closer.

The problem that I get is when there is an obsticle in the way, the unit cannot recognise that there is anything there. I have investigated breadth-first pathing, but I just don't understand it very well.

Does anybody know how I can create a basic pathing script for my AI?

Cheers

About the author

Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.


#1
02/19/2008 (12:40 pm)
There's a few ways to go about doing this, if you're really interested. I've been doing a bit of scripting for AI stuff for a while now. I haven't got it completely implemented, but AI kinda depends on the situation you're looking to resolve. One albeit cheap method, would simply to tell your AI to move to the player's last move position. So, for instance, your player can have a couple of variables such as currentPos, and lastPos, and the currentPos would be where the player is at, and the lastPos would be where he came from. This would allow you to send the AI follower consistently to the lastPos without worrying about it being stuck. otherwise, you might need callbacks on the AI unit.. Let me know if you'd like help with this..
#2
02/21/2008 (12:32 am)
Check out the 'moveTo' function in the API docco. You can simply give it a position and the entity will move to it. It will not, however, guide itself around any obstacles in the way if you have collisions suitable enabled on the object, and you will have to implement some kind of pathing behaviour. Breadth and depth first searching for AI purposes will 'work' but are generally way too slow for anything beyond trivial. Dijkstra's [url]http://en.wikipedia.org/wiki/Dijkstra's_algorithm#Running_time[/url] might be a little bit better also, but what you really what to look into is the A* algorithm.
#3
02/21/2008 (12:44 am)
I've since dropped this project, but for anyone who is interested I found a resource created by Phil Shenk which implements A* into TGB, which works without mods in 1.7.1.

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9711