Okay, I know this should be much more straightforward than I'm finding but my brain is mush today, sorry.
In the level i'm making I want characters to patrol back and forth over an area repeatedly - specifically travel through a set of waypoints. I've been reading up on Angry Ant's Path and Aron Granberg's A* Pathfinding for a future project, but for this I don't need actual pathfinding since the characters don't have to go anywhere other than along their set path. They will have a vision cone or torchlight that, when it touches the player, will reset the players position. Basically a really simple stealth game.
Last year I modified the SeekSteer script on the wiki to have a character move along a set of waypoints but it was a bit of a rushed job and it had some problems. I'm returning to it now and starting from scratch. I'm using the Lerp function as described in this post to move my object to the new position and i've combined that script with my waypoint one (it looks nothing like SeekSteer now by the way).
I rewrote my old script since I had the unwanted effect of easing in and out of positions - where that came from I don't know - but aside from that it worked. I'm using the same code here, the only real difference is the method to move the character and the old script had a radius for each waypoint. There are currently 3 transforms in the waypoints array to make an 'L' shape. But after the character reaches the first waypoint I get an Index Out of Range error.
As far as I can tell, the part where the targetwaypoint variable is being incremented is suspect but this wasn't a problem in the other script despite being in the Update function.
asked Mar 10 '10 at 04:30 PM
I think your "super fast" problem is occuring because you're not resetting the startTime variable when you increment the targetwaypoint counter. This means that for every transition after the first one, the duration has already elapsed.
As for the 'index out of range' error, I would guess that's occuring because you're assigning the new endPoint before you check whether the targetwaypoint variable should be wrapped back to zero.
Move the line:
...to after the check where you test if it's the last item in the array. (remember, array access is zero-indexed, so if targetwaypoint == waypoints.Length, then targetwaypoint isn't a valid index!
(If your array is 8 items long, the first item is at position zero, and the last item is at position 7!)
Also, you might be interested in a neat way of "wrapping" your integer index counter using the modulus operator.
Also, I've moved the Lerp interpolation value to a separate variable for shorter line length, and fixed the indentation :)
So, with these fixes, your could might look like this.
I've been looking into stepping back through the array when I reach the end. I didn't achieve that yet (seemed to have become a bit distracted in the process!) but I've modified my code quite a bit so thought I would post it here as an alternate solution.
Currently I have an empty object called waypoints with child cubes for the individual waypoints. The script is attached to the NPC and so has to find the waypoint object's children (took me a while to get that working!). I'd be happy to hear and comments/criticism, and thanks once again to you Duck for your help.
answered Mar 11 '10 at 02:36 AM