|
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.
(comments are locked)
|
|
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. Thanks for the detailed response Duck=) I knew about arrays being zero indexed but didn't see the flaw in my logic, and yeah, I should have noticed the problems with startTime and endPoint. I looked at the modulus operator you mentioned and, while I understand what it does with two values, i'm not sure I understand what it's doing here (that and I get an unexpected token error when using it). I'm assuming the result wraps around back to '0' each time? How would I write this without the shorthand? Ultimately I want the character to move through the waypoints in reverse when they reach the end.
Mar 10 '10 at 08:17 PM
straydogstrut
I was a bit confused at first with the line if(i >= 1) but am I right in thinking that this is doing a similar thing as my line if(transform.position == endPoint) since the '1' is the end value returned by the Lerp function (or at least, as I understand it, the value of i varies between 0 and 1)? Is there any benefit to writing it this way instead apart from shorter code? I also couldn't assign the temporary variable 'i' like you have - in the end I had to create a variable for it - as it kept asking for a semicolon.
Mar 10 '10 at 08:22 PM
straydogstrut
Oh, yes - the i>=1 is mainly for shorter code, but it's also generally good practice to not rely on comparing floats (or structs which contain floats) for equality, because of floating point imprecision problems: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Mar 10 '10 at 08:27 PM
duck ♦♦
I'm surprised that %= doesn't work - it seems to be yet another thing missing from the JS syntax. Basically: "a %= b" is the same as "a = a % b", in the same way as "a += b" is the same as "a = a + b". And the "float i" thing was a result of me accidentally mixing c# and JS. sorry! (both have been fixed in the code now)
Mar 10 '10 at 08:28 PM
duck ♦♦
Thanks Duck, that fixes the errors and i'm getting there with understanding % (maths was never my strong point - back to school I think!) Cheers.
Mar 10 '10 at 09:05 PM
straydogstrut
(comments are locked)
|
|
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. Now i'm using a normal Javascript array rather than an in-built one (although i've read in-built ones are faster). This approach allows me to use the Shift and Add functions to achieve the same effect as Duck's modulus approach (as far as I can tell) and also means that the array is resizeable. Since Javascript arrays can't be modified from the editor i've added some script to automatically add the waypoints to the array at initialisation (much more efficient than dragging them into the editor). 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.
(comments are locked)
|

I've just read through my post again and realised I don't actually change the value of startPoint at any point after it is initially set. Adding startPoint = transform.position; into the Update function has slowed it down enough that I can see all the waypoints are being used but after the first one it's super fast and I still get an Index Out of Range error at the end. Am I going about this completely the wrong way?