Waypoints
September 29, 2009by 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();
allWaypoints.Add(currentWaypoint);
}else{
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.
September 29th, 2009 at 8:30 pm
theTarget, by the way, is the target which the AI chases. allWaypoints is the array of game objects tagged with “Waypoint”.
October 21st, 2009 at 3:50 pm
Nate: FYI if you edit your post in html (there’s a tab for this in the message composition interface) then you can put
after it to make it display in a monospaced font with whitespace preserved, which makes code more readable…
October 21st, 2009 at 3:52 pm
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…
October 21st, 2009 at 3:54 pm
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.