Help With Race Ranking System - Place switching from first to second when AI's cross the finish line
So I have created a car AI system where it ranks the cars based on their place. It works great until an AI car crosses the finish line. It switches your place to 2nd and then back to 1st, in the time after they cross the finish line to the time they cross the 1st checkpoint. Any ideas about what's going on?
Here's a sample of my code:
//LapCount.js
function Rank () {
var dist = Vector3.Distance(checkpoint[checkpointId].transform.position, transform.position);
var dist1 = Vector3.Distance(checkpoint[checkpointId].transform.position, AICar1.transform.position);
if (lap < AILap) { // If lap is greater or less than
isBehind1 = 1;
}
if (lap > AILap) {
isBehind1 = 0;
}
if (lap == AILap) {
if (checkpointId < AICP) { // If AI Checkpoint (AICP) is greater or less than
isBehind1 = 1;
}
if (checkpointId > AICP) {
isBehind1 = 0;
}
if (checkpointId == AICP) {
if (dist < dist1) { // If distance from checkpoint is greater or less than
isBehind1 = 0;
}
if (dist > dist1) {
isBehind1 = 1;
}
}
}
}
Answer by ananasblau · Oct 22, 2016 at 04:53 PM
First of all, do the dist and dist1 calculations down in the lab==AILap, checkpointId==AICP, no need to do them every time.
Secondly, could it be that the checkpointId and AICP aren't being reset properly at the finishlin? I do hope the finish line is derived from checkpoint (if not this is likely why the checkpoints are off).
Thank you for your reply. It was because I had a separate finish line that reset the checkpoints. I solved it by making the 1st checkpoint really close to the finish line. That seemed to solve it. Thank you for your help
I also manually called rank every .5 seconds using Invoke ins$$anonymous$$d of FixedUpdate or Update
Your answer
Follow this Question
Related Questions
Spriting gone wrong?! - Unity 2D 0 Answers
gameobject turns to None when game play 1 Answer
Detecting Button Click 1 Answer
How to fix CS0246 Error 0 Answers