- Home /
Making a race car positioning system
My game is nearly complete, I even have ai cars. I just dont know how to make a car positioning system for the players. I mean like 1st 2nd 3rd..... Can somone explain how to do that? Im not asking for someone to just give me a script, I would prefer a tutorial of some type. As long as I understand whats happening in the script..... C# please, but java is also acceptable. I know theres somthing in the asset store you can buy(its expensive), but im looking to learn, not own.
If i understand correctly, do you mean that you can position each car and it will be perfectly placed the moment you select it despite its size difference?
no, im looking for a way to calculate the distance of each car from the first car, organized in order from least to greatest.
trigger enter is triggers when 2 collider triggers "collide"
yea you start the checking upon trigger enter the matching function recieves a car and then toy trigger collide with other:Collider you do the checking with this other.
since trigger stay check happen every frame - means 30 times a sec or so, you do it in no time
Answer by bubzy · Dec 02, 2014 at 05:49 PM
make gameobject prefabs, give them the tags "firstPlace" "secondPlace" etc
when you instantiate the cars, search for the gameobjects and use their transform to position the car.
We are trying to figure out who's closest to the finish line while the race is happening
Answer by Baste · Dec 02, 2014 at 05:57 PM
Here's one way I can think of:
1: Place checkpoints at regular intervals along the track. Put them in a ordered list of some sort, so you know their ordering.
2: Have each car know which checkpoint is the last one it passed. You can do this by distance checking, or by triggers, or something else that's clever.
3: Rank cars by which checkpoint they passed last. If two cars' last checkpoint is the same, rank them by their distance to the next checkpoint.
That should probably cover the basics. There's a lot of fine tuning to do, but that's depending on the game. Oh, and I think you'll need a checkpoint just before and just after every turn to make this work - otherwise the distance calculations will be weird.
Hope that helps!
@Baste 's approach is good.
In a Facebook racing game i was a programmer in, we used an advanced version of this. We had a level editor with track pieces to build with. Each track piece had a few "waypoints" built into the 3D model. When going to race, we dynamically parsed the list of waypoints from start to finish to get a list of nodes signifying a complete lap.
When racing, I would use (surprisingly) simple vector math in Update() to project the cars position onto the node/vector representation of the track. This way we could calculate the car's progress on the track "analogously", not just update it when it moves over a certain node/collider. It also gave us a pretty fool proof way of tracking whether the car cheated by taking a short cut or something.
If you are planning on having branching in the track or more freedom of movement in some other form, this approach might get very complicated quickly.
I'm thinking I might have an object look at the player, and the course progress is deter$$anonymous$$ed by that objects local rotation
Answer by nirharpaz · Dec 02, 2014 at 07:24 PM
my idea would be then: give a rank to each car
int rank;
that is initialized at the start according to position. add a trigger collider / other proximity sensor to each car.
when 2 cars are in sensor (collider could be horizontal thin box, 90deg from the ground, creating horizontal cross with the road) OnTriggerStay - monitor their position who is 1st by angle (/ shape means 2nd in before, \ sape means 1st is before) OnTriggerExit - stop those calcs.
also, if box is thin enough and you dont need the updated rank every given sec you can just update when they leave trigger.
might be clumsy but it will give u an updated info in any given moment.
Answer by nnadiwisdom1 · Nov 08, 2020 at 12:13 AM
if you need a solution check this link https://youtu.be/eKFVTLO6-E4
Answer by kapyar · Feb 09, 2016 at 05:14 PM
Hey, you could try my plugin which can handle all math. Its really simple to use you must add only 2 prefabs to your scene https://www.assetstore.unity3d.com/en/#!/content/45698
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to set speed limit of car? 3 Answers
Car- can someone help me convert this to C#? 1 Answer
where can i find a series of video c# tutorials or javascript tutorials 3 Answers