I got the old pathfinding script from http://www.arongranberg.com/unity/pathfinding/ but it uses a clicked point where to move to, but i don't know what is to be changed, so that i don't have to click the enemy first and a end point, i want an enemy to follow the player at all time.. anyone can help?
Here's the script:
// Copyright 2009 - Aron Granberg - aron.g@me.com
//This is version 0.5 of the script
var points : Array;//The nods
var pathlength : float = 0;
var giz = false;
var mask : LayerMask;//Use this to avoid raycasting units or terrain
var minimumdistance : float = 3;//The minimum distance from the target it must be to get finished.
private var level = 0;
private var maxloops = 40;//The max number of times the findpath function can be called (to prevend hanging)
var debugg = true;
function startFind (fpos : Vector3, pos : Vector3, margin : float) {//This is the function you shall call, don't call fundPath function
giz = false;
points = new Array();
points.Add (fpos);
level = 0;
findPath (fpos,pos, margin,Vector3.zero);//Calculate the path
yield WaitForEndOfFrame ();
giz =true;
var r = points.ToBuiltin(Vector3);
var re = GetComponent (AI);//This is the script for the units
if (re) {//Does it have the AI script attahed
re.points = r;
re.FindPoint (0);
}
}
function findPath (fpos : Vector3 , pos : Vector3, margin : float,contwith : Vector3) {
level += 1;
if (level > maxloops) {//Just so it does not hang if the point is in a ungetable place (like inside a box)
//print ("Chrash Protection, to many loops");
return;
}
if (Vector3.Distance (fpos, pos) < minimumdistance) {//Is it there yet
if (contwith != Vector3.zero) {
findPath (fpos,contwith,margin,Vector3.zero);
} else {
return;
}
}
var hits : RaycastHit[];
var hit : RaycastHit;
var p : Vector3 = pos - fpos;
hits = Physics.RaycastAll (fpos, p, p.magnitude, mask);//Objects between start and target
if (hits.length == 0) {//If we can see the target it is just to go straight to it
Debug.DrawLine (fpos, pos, Color.green);
points.Add (pos);
if (debugg)
//print ("Free Way");
pathlength = 0;
for (i = 0;i<points.length-1;i++) {
pathlength += Vector3.Distance (points[i],points[i+1]);
}
return;
}
Debug.DrawLine (fpos, pos, Color.red);
var gos = new Array ();
for (i=0;i<hits.length;i++) {
//print (hits[i].transform.gameObject.name);
gos.Add (hits[i].transform.gameObject);//Put them in a array
}
var mindist = Mathf.Infinity;
for (i=0;i<gos.length;i++) {
dist = Vector3.Distance (fpos, gos[i].transform.position);
if (dist < mindist) {
tg = gos[i];// The nearest gameObject
}
}
point = findpoint (tg,fpos, pos, margin);
if (point == Vector3.zero) {//If findpoint function returned zero it failed
if (debugg)
print ("Failed");
return;
}
if (point == Vector3.one) {//If findpoint function returned zero it failed
return;
}
points.Add (point);//Add the calculated point and run the function again
findPath (points[points.length-1], pos ,margin,contwith);
}
var cubep : Array;//Just an array
function findpoint (tg : GameObject, fpos : Vector3, pos : Vector3, margin : float) : Vector3 {
cubep.Clear ();
bo = tg.collider.bounds;
ext = bo.extents;
ext.y = 0;
cubep.Add ( bo.center + ext + (ext.normalized * margin));
ext = bo.extents;
ext.y = 0;
ext.x = -ext.x;
cubep.Add ( bo.center + ext + (ext.normalized * margin));
ext = bo.extents;
ext.y = 0;
ext.z = -ext.z;
cubep.Add ( bo.center + ext + (ext.normalized * margin));
ext = bo.extents;
ext.y = 0;
ext.x = -ext.x;
ext.z = -ext.z;
cubep.Add ( bo.center + ext + (ext.normalized * margin));//Calculate 4 points around the object and add a margin
v = new Array ();
for (i=0;i<cubep.length;i++) {
if (cubep[i] != points[points.length-1]) {
if (!Physics.Linecast (fpos,cubep[i], mask)) {///Is the point visible from here?
Debug.DrawLine (fpos,cubep[i], Color.green);
Debug.DrawRay (fpos,cubep[i].normalized * 40,Color.red);
v.Add (cubep[i]);
} else {
Debug.DrawLine (fpos,cubep[i], Color.red);
}
}
}
if (v.length == 0) {//We can't see any points, then calculate the path to the best point
var px : Vector3 = findpossiblepoints (pos,fpos,tg,cubep);
findPath (fpos,px,margin,pos);
return Vector3.one;
}
var mindist : float = Mathf.Infinity;
var point = Vector3.zero;
for (i=0;i<v.length;i++) {
var d : float = Vector3.Distance (pos,v[i]);//Which one is closest?
if (d < mindist) {
mindist = d;
point = v[i];
}
}
return point;
}
function findpossiblepoints (pos : Vector3,fpos : Vector3, tg : GameObject, cubep : Array) : Vector3 {
//print ("Help called");
var v = new Array();
for (i=0;i<cubep.length;i++) {
var dir : Vector3 = cubep[i] - fpos;
var ra : Ray = Ray (fpos,dir);
if (!tg.collider.bounds.IntersectRay(ra)) {//Is it intersecting the tg/Should it be visible (if it wasent some things in the way)
v.Add (cubep[i]);
Debug.DrawRay(fpos,dir,Color.yellow);
}
}
var mindist : float = Mathf.Infinity;
var point = Vector3.zero;
for (i=0;i<v.length;i++) {
var d : float = Vector3.Distance (pos,v[i]);//Which one is closest?
if (d < mindist) {
mindist = d;
point = v[i];
}
}
return point;
}
function Awake () {
giz = true;
points = new Array();
cubep = new Array();
}
function Update () {
for (i = 0;i<points.length-1;i++) {
Debug.DrawLine (points[i],points[i+1],Color.red);
}
}
function OnDrawGizmos () {//Just some debug info
if (giz && debugg) {
for (i = 0;i<points.length-1;i++) {
Gizmos.color = Color.green;
Gizmos.DrawLine (points[i], points[i+1]);
Gizmos.color = Color.yellow;
Gizmos.DrawSphere (points[i], 0.5);
}
for (i = 0;i<cubep.length;i++) {
if (i==0) {
Gizmos.color = Color.red;
} else {
Gizmos.color = Color.green;
}
Gizmos.DrawSphere (cubep[i], 0.5);
}
}
}
asked
Feb 20 '11 at 09:12 PM
Raymond 2
119
●
37
●
40
●
45