Hi there,
I have an array of waypoints, I am trying to move from point to point in order of the ones visited the longest time ago, I have an array which i cycle through looking for points I can see and then I move my toon toward it, when it gets there, the point is moved to the bottom of the list and I go for the next one. The code is below. Sometime it works and sometimes it return a null even though I know a point is visible. There’s a bug there somewhere but I just can’t get it. is my ray striking my toon’s character controller maybe? any thoughts would be good.
Thanks all
#pragma strict
#pragma downcast
var closeEnough : float = 8.0; // distance where we are clo9se enough to a point to go to the next
private var wayArray : Array; // an arrray of waypoints
function Start()
{
// collect all of the waypoints
wayArray = new Array(GameObject.FindGameObjectsWithTag("WayPoint"));
}
function FindOldestWaypoint () : GameObject {
var hit : RaycastHit;
var oldest: GameObject;
var position = transform.position;
var lp : int;
// Iterate through them and find the top of the list
for (lp = 0 ; lp < wayArray.length; lp ++)
{
var go : GameObject = wayArray[lp];
// raycast, check for intervening objects, can I see it?
var raydirection = go.transform.position - position;
// if the waypoint we are testing has a collider blocking it the ignore it0
if (!Physics.Raycast (transform.position, raydirection, hit))
{
// draw a debug ray
Debug.DrawRay(transform.position, raydirection, Color.red); // debug code
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < closeEnough) {
// if we are close engough, the current waypoint is moved to the bottom of the list
wayArray.RemoveAt(lp);
wayArray.Push(go);
}
oldest: = wayArray[lp];
break;
}
}
return oldest:
}