x


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: 
}
more ▼

asked Jun 08 '12 at 11:24 PM

hatchnet gravatar image

hatchnet
65 3 7 9

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.

Jun 08 '12 at 11:52 PM bompi88

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 instead.

You might also want to reference the oldest point before changing the array.

Jun 09 '12 at 09:05 AM asafsitner
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;

}

more ▼

answered Jun 09 '12 at 11:04 PM

hatchnet gravatar image

hatchnet
65 3 7 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1526
x1358

asked: Jun 08 '12 at 11:24 PM

Seen: 411 times

Last Updated: Jun 09 '12 at 11:04 PM