- Home /
Waypoint problems
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:
}
why is it
oldest: = wayArray[lp];
and not?
oldest = wayArray[lp];
And "return oldest:" should be "return oldest;". Another thing: format your code in the question with selecting all of your code and hit 101010, till it formats well. Then I will take a closer look at it.
Not sure this is what causes the problem, but you might as well fix it while you're at it - normalize your ray direction vector, or use it's normalized
property within the raycast call.
You can get rid of 'diff' as you've already calculated the same vector for the raycast direction, so you can use that ins$$anonymous$$d.
You might also want to reference the oldest point before changing the array.
Answer by hatchnet · Jun 09, 2012 at 11:04 PM
Thanks for the suggestions guys, the recalculated vector was an oversight, the : in the wrong place seems to have been because of the way I copied and pasted the code.
I solved the problem by placing the playing surface on the ignore raycast surface, a new concept for my newbie self. I found the problem by putting lots of debug statements in to find out what the ray was colliding with and hey presto, it was the playing surface.
Oh, and I didn't know about the 101010 button. Here is my completed code
Cheers
function FindOldestWaypoint () : GameObject {
var hit : RaycastHit;
var oldest : GameObject;
var distance = Mathf.Infinity;
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, can I see it?
var raydirection = go.transform.position - position;
Physics.Raycast (transform.position, raydirection, hit);
// 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
// get the distance to the waypoint and if it's close, move it to the end of the array
// and go for the neat one
var curDistance = raydirection.sqrMagnitude;
if (curDistance < closeEnough) {
// if we are close engough, the current waypoint is deleted and
// moved to the bottom of the list
wayArray.RemoveAt(lp);
wayArray.Push(go);
}
else
{
oldest = wayArray[lp];
break;
}
}
}
return oldest;
}
Your answer
Follow this Question
Related Questions
Raycast cycle problem? 0 Answers
Array index is out of range and Raycast question 0 Answers
Return a List of Gameobjects at the mouse position 0 Answers