Hi,
I am making a racing game and need help with with the code for knowing if the player is driving the wrong way.
I’m not sure how to implement this?
Any help is greatly appreciated .
Thanks
Hi,
I am making a racing game and need help with with the code for knowing if the player is driving the wrong way.
I’m not sure how to implement this?
Any help is greatly appreciated .
Thanks
I don't think the angle method is the best way to solve this. In addition to the winding road-problem save pointed out, the angle method fails even on a straight road if I decide to drive backwards, right? You're not necessarily going in the wrong direction just because your vehicle faces the wrong direction, nor are you necessarily going forward just because it faces forward. I propose something different:
Define the road as a series of points, just like piecing together a mathematical function by drawing straight lines between a number of points in a coordinate system. Store these points in an array, so that index 0 is the start line and the final point is the goalpost. Then you can use relatively simple vector calculus to determine which of the points is currently closest to the vehicle, and save the index of that point in your array. Now, if the index of the point closest to the vehicle ever DECREASES... then it means the vehicle is now suddenly closer to a point behind it than it was last time you checked, i.e. its approximate distance to the goal has increased again, so it must be going backwards.
This solves the angle problem I mentioned, but a word of caution: It will totally fail if the car can freely leave the road. If it's possible to leave the road, and the racetrack is shaped so that stretches of road close to the goal curves around and becomes close to earlier pieces of road, it would be possible to trick the algorithm into thinking you're going backwards when in truth, you're stilling heading mostly toward the goal.
If the racetrack is enclosed so you can't leave it, however, this approach is fine.
What if you had two invisible walls around the track and did a raycast to the right. If the raycast hits the inner wall you are going the right way. If your raycast hits the outer wall and you are going forward you are going the wrong way.
An ideia is to make several “gaps” or “checkpoints” with triggers, this way you can show the time diference between opponents, part of the lap time diference and know if the player is driving the wrong way.
This may not be the best way, but probably you will need to have these “checkpoints”, so, you can reuse them.
The ckeckpoints have an order, like:
chp001 → chp002 → chp003
So, if the player cross the chp002, them he need to cross the chp003, if not, he is on the wrong way.
Hope this help,
Borgo
This sounds about right, I'd also check angle from player to next checkpoint, if angle is > ~-120 to ~120 then reset when player comes back within ~-90 to ~90 (this can not be done though on vast winding road structures with checkpoints far from each other). Or simply if the latest old checkpoint is in view again then it's the wrong way. Just an idea!
– saveYes to the angle idea. The problem with checking crossing order is you could get turned around just before C5, so not get a message until you drive all the way back to C4, when you should get a message right away. A coroutine, every 2 seconds, could just check the "angle to next" is less than 90.
– Owen-ReynoldsThank you I will give it a go ![]()
Hey there people,
It seems that I have solved MY racing backwards problem. Although this is a little
late, check out the video’s on my YouTube channel.
My YouTube Channel with Unity3D Racing video’s
+1 for great elaboration!
– saveTurns out this is the same as Borgo's idea. Using "closest checkpoint" instead of "last passed checkpoint" just shifts each "checkpoint zone" by 1/2. If the checkpoints are 50 meters apart, you still may have to drive 50 meters to change the one you are nearest to.
– Owen-ReynoldsIt doesn't have to be the same as Borgo's idea. If you know which two indices you're in between, you can track the actual distance to the next as well. If that also increases, you're going the wrong way. This can be determined at any time, but you'll need an increased number of checkpoints in curves.
– CHPedersenThat would be a little faster -- no angle math needed. Essentially a standard "waypoint" system with an "am I getting closer to next WP?" check.
– Owen-Reynolds