- Home /
OnTriggerEnter/Exit at speed
I use a simple waypoint system with a .next pointer to advance to the next waypoint as my vehicle passes through a waypoint trigger.
I use this with an OnTriggerEnter to trigger when my vehicle passes through a waypoint, which all works fine, however, I don't want my vehicle to be able to stop in a waypoint and reverse out of it or turn around within it and head from where it came, I'd want to be able to track that. This would leave my waypoints list out of sync with where my vehicle is heading and I use the waypoints as triggers for a sequence of events to occur, such as if its the start/finish line -> increase the lap count, if waypoint is waypointN -> open shortcut path etc...
I've tried to make the waypoint trigger area as thin as possible, but then its possible that the OnTriggerX event doesn't fire at all - I also have a small collider/trigger on the front of my vehicle.
I'm sure I could write some convoluted check in the event method(s), but is there any recommended approach to take when working with OnTriggerX with waypoints?
Answer by Novodantis 1 · Jun 10, 2010 at 10:56 AM
When you register the trigger collision, store the waypoint object in a variable on the vehicle. Your trigger collision should check to see if the collided object is not the waypoint stored in your variable. This should at the very least ensure you only register a waypoint once (make them big so they definitely register).
As for going backwards, you could then add a subsequent check to see if the waypoint passed is further along the track than the one you stored. Next waypoint if so. And if not, display a large sign saying they are going the wrong way or something? =)
Thanks, I was being too ahead of myself with a next/current/prev waypoints variables to check against, and re-addressed my logic. I used ClosestPointOnBounds() to get the exit point in OnTriggerExit() and tested for this position to be within a region directly after or before my single waypoint variable. Works like a charm.
I also had to make the waypoints sufficiently large so as to not miss a trigger.
Your answer
Follow this Question
Related Questions
Clearing a destination... 1 Answer
Using box collider to determine if AI should stop help 1 Answer
Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer
Help with a simple AI 2 Answers
How to move to random position? 1 Answer