my AI target/path script (update: youtube video demo)

ok this could probably be cleaned up a lot but it still works great for what im using it for.

var path : Vector3[];
private var returnPath : Vector3[];
var key : int = 0;
private var target = Vector3(0.0,0.0,0.0);
private var rparr = new Array (Vector3);

function Start() {
    target = path[key];
}

function Update() {
    if (Vector3.Distance(transform.position,target) < 1) {
      if (target == path[key]) {
         key = key + 1;
	   }
      if (key >= path.length) {
         key = 0;
      }
		if (returnPath == null || returnPath.length == 0) {
         target = path[key];
		} else {
		   if (target == returnPath[0]) {
			   rparr = new Array (returnPath);
			   rparr.RemoveAt(0);
				returnPath = rparr.ToBuiltin(Vector3);
			}
			if (returnPath != null  returnPath.length != 0) {
				target = returnPath[0];
			} else {
				target = path[key];
			}
		}
   }
   if (returnPath == null || returnPath.length == 0) {
		if (Physics.Linecast (transform.position, path[key])) {
			rparr = new Array (transform.position);
			returnPath = rparr.ToBuiltin(Vector3);
	   } 
   } else {
		if (Physics.Linecast (transform.position, returnPath[0])) {
			rparr = new Array (returnPath);
			rparr.Unshift(transform.position);
			returnPath = rparr.ToBuiltin(Vector3);
		}
	}
	if (returnPath != null  returnPath.length != 0) {
	   for(var rp = 0; rp < returnPath.length; rp++) { 
	      if (!Physics.Linecast (transform.position, rparr[rp])) {
		      var drp : int = rp;
		      while (drp > 0) {
			      drp--;
				   rparr = new Array (returnPath);
			      rparr.RemoveAt(drp);
				   returnPath = rparr.ToBuiltin(Vector3);
				   rp = 0;
			   }
		   }
	   }
	   if (!Physics.Linecast (transform.position, path[key])) {
	      rparr = new Array (returnPath);
	      rparr.Clear();
	      returnPath = rparr.ToBuiltin(Vector3);
	   }
	}
}

also currently the player object and the objects with the AI are set to ignore raycast in order to get this to work. you may have to tweek this if you want to get this to work or just do what i did.

the ai works by setting an array for a path. when the object with the script gets close enough to the target if the target is currently the same path[key] then it will change the key and set a new target to go to. if it casts a linecast between it and the target and there is an object in the way (which if you dont set the player and the AI object to ignore raycast or if you dont edit the script and object to prevent the the linecast from ignoring them it will detect the player or the AI object as something in the way and wont work) it will set a return path and add the position it lost sight of its target to index 0 of the array. if it loses sight of the last return path position it will add to the array if it sees its path[ ] target or any point in the return array it will cut the unnecessary points in the array to make a shorter return path. (the return path is for stuff like if the AI attacks a player and follows the player out of sight of its path target). if it reaches a target that something in the script forces it to target (like a player) (which would happen if the player dies) if it lost sight of its path target it will follow the return path back to its target and wont get stuck running against a wall blocking it from its path target.

my script probably could use some improvement but at least it works.

please tell me what you think.

EDIT:
here is a youtube video demo of my script

does anyone have a comment about my script?

I’d like to see an example web player. Right now, it’s hard for me to create a new project just to test it.

you have a good point. i may make a youtube video and example project.

Thanks Drazil… this looks very interesting, and a video would be helpful.

got a youtube video now

So, it generates a path without any waypoint game objects? That’s pretty useful.

pretty cool~
thank you~

Thank You! are we allowed to modify and build upon?

how is this project going, any updates?