September 29, 2009
by Nathan Whitehouse (nw08)

Are working. All objects tagged “Waypoint” are dynamically added to an array of waypoints. When the AI opponent comes within a set distance of the target waypoint, that waypoint is moved to the back of the array, and the new top waypoint becomes the target destination.

Of course, if the player gets near, the AI abandons the waypoints and chases the player until he manages to escape, in which case it’s back to the waypoints.

function FindNextWaypoint(){
var currentWaypoint : GameObject;
var distanceToWaypoint : float;
distanceToWaypoint = Vector3.Distance(transform.position, allWaypoints[0].gameObject.transform.position);
if(distanceToWaypoint < 10){
theTarget = allWaypoints[1];
currentWaypoint = allWaypoints.Shift();
theTarget = allWaypoints[0];

FindNextWaypoint is called when the player leaves the AI’s hearing radius. A logical next step would be to make the AI start with the nearest waypoint when the player eludes it, rather than the last waypoint it was on in the series.

4 Responses to “Waypoints”

  1.   nw08 Says:

    theTarget, by the way, is the target which the AI chases. allWaypoints is the array of game objects tagged with “Waypoint”.

  2.   lspector Says:

    Nate: FYI if you edit your post in html (there’s a tab for this in the message composition interface) then you can put

     before code and 

    after it to make it display in a monospaced font with whitespace preserved, which makes code more readable…

  3.   lspector Says:

    Ha! It interpreted the tags that I put in that previous message! So it put “before code and” in the monospace font… The opening tag that you put before the code is this but without the extra spaces I inserted to trick it, and the closing tag that you put after the code is this but without the extra spaces…

  4.   lspector Says:

    Oy! It mangled that too — I didn’t successfully trick it. The opening tag is the left angle bracket (also known as the “less than” symbol) followed by “pre”, followed by the right angle bracket (also known as the “greater than” symbol). The closing tag is the same thing but with a slash (also used as a division symbol) after the left angle bracket. Whew.

Leave a Reply

You must be logged in to post a comment.