How to make race positioning system work for more than two racers

I’m working on a 3D kart racing game. The game uses a standard 3-lap system. To achieve this, I’ve created 8 invisible checkpoints around the track called “Position Hitters”. Every time a Racer collides with a Position Hitter, one “position hit” is added to that racer’s score. Each Hitter has a score requirement (ex: you must have “2 Position Hits” to gain a point when driving through the third hitter) so players can’t cheat and go backward. After hitting eight Position Hits, one lap is added to every racer’s score. This is working perfectly (as shown below):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionHitter : MonoBehaviour
{
    public GameObject Checkpoint1;
    public GameObject Checkpoint2;
    public GameObject Checkpoint3;
    public GameObject Checkpoint4;
    public GameObject Checkpoint5;
    public GameObject Checkpoint6;
    public GameObject Checkpoint7;
    public GameObject Checkpoint8;
    public GameObject nearest;
    public int positionHits = 0;

    void Start()
    {
        nearest = Checkpoint1;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "CP1")
        {
            if (positionHits == 0 || positionHits == 8 || positionHits == 16)
            {
                positionHits += 1;
                gameObject.transform.parent.GetComponent<RacerInformation>().currentLap += 1;
                Debug.Log(gameObject.transform.parent.GetComponent<RacerInformation>().Name + " Lap: " + gameObject.transform.parent.GetComponent<RacerInformation>().currentLap);
                nearest = Checkpoint1;
            }

            if (positionHits == 24)
            {
                positionHits += 1;
                Debug.Log(gameObject.transform.parent.GetComponent<RacerInformation>().Name + " finished at " + positionHits + " position hits.");
            }
        }

        if (other.gameObject.tag == "CP2")
        {
            if (positionHits == 1 || positionHits == 9 || positionHits == 17)
            {
                positionHits += 1;
                nearest = Checkpoint2;
            }
        }

        if (other.gameObject.tag == "CP3")
        {
            if (positionHits == 2 || positionHits == 10 || positionHits == 18)
            {
                positionHits += 1;
                nearest = Checkpoint3;
            }
        }

        if (other.gameObject.tag == "CP4")
        {
            if (positionHits == 3 || positionHits == 11 || positionHits == 19)
            {
                positionHits += 1;
                nearest = Checkpoint4;
            }
        }

        if (other.gameObject.tag == "CP5")
        {
            if (positionHits == 4 || positionHits == 12 || positionHits == 20)
            {
                positionHits += 1;
                nearest = Checkpoint5;
            }
        }

        if (other.gameObject.tag == "CP6")
        {
            if (positionHits == 5 || positionHits == 13 || positionHits == 21)
            {
                positionHits += 1;
                nearest = Checkpoint6;
            }
        }

        if (other.gameObject.tag == "CP7")
        {
            if (positionHits == 6 || positionHits == 14 || positionHits == 22)
            {
                positionHits += 1;
                nearest = Checkpoint7;
            }
        }

        if (other.gameObject.tag == "CP8")
        {
            if (positionHits == 7 || positionHits == 15 || positionHits == 23)
            {
                positionHits += 1;
                nearest = Checkpoint8;
            }
        }
    }
}

Now if you’ll notice, there’s a GameObject in this script called “Nearest”. So for my positioning system, I can’t just compare the Position Hit scores. I need to know which racer is in the lead when two or more cars have the same score. This way, I can detect the racer’s distance to the next Position Hitter and have it change constantly depending on the racer’s position hit score.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RacerInformation : MonoBehaviour
{
    public string Name;
    public int currentLap;
    //This script will pull in variables from the RacerStats script: Top Speed, Handling, and Acceleration. Maybe Weight.
    public int playerPos;
    //public GameObject[] PositionHitters;
    public GameObject nearestHitter;
    public float nearestDist;

    void Start()
    {
        //PositionHitters = GameObject.FindGameObjectsWithTag("HitStore");
        nearestHitter = gameObject.transform.GetChild(1).GetComponent<PositionHitter>().nearest;
    }

    void Update()
    {
        nearestHitter = gameObject.transform.GetChild(1).GetComponent<PositionHitter>().nearest; //This was causing the issue. The start wasn't enough(). This needs to be pulled in frame by frame as it is
        //updated in the other script frame by frame.
        nearestDist = Mathf.Abs(Vector3.Distance(transform.position, nearestHitter.transform.position));
    }
   
}

So here’s where the problem lies. The script below is where the positioning is actually taking place. I know that doing something iterative in an Update() is not considered good practice but I need to constantly update the positions during the race. I used a loop within a loop so every racer is compared to every other racer. When the position hit scores are the same, the edge will go to the racer closest to the next hitter.

This algorithm is working perfectly when two karts are racing. But when I added a third one, it seemed to confuse the algorithm. None of the racers are being assigned 2nd place. Just 1st and 3rd, and even those positions are being updated properly.

Is there something I’m missing here?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Positioning : MonoBehaviour
{
    public GameObject[] Racers;

    // Start is called before the first frame update
    void Start()
    {
        Racers = GameObject.FindGameObjectsWithTag("Pos");
    }

    // Update is called once per frame

    void Update()
    {
        for (int i = 0; i < Racers.Length; i++)
        {
            for (int j = i + 1; j < Racers.Length; j++)
            {
                if ((Racers*.transform.parent.gameObject.transform.GetChild(1).GetComponent<PositionHitter>().positionHits > Racers[j].transform.parent.gameObject.transform.GetChild(1).GetComponent<PositionHitter>().positionHits))*

{
GameObject go = Racers*;*
Racers = Racers[j];
Racers[j] = go;
Racers*.transform.parent.GetComponent().playerPos = Racers.Length - j;*
}

else if ((Racers*.transform.parent.gameObject.transform.GetChild(1).GetComponent().positionHits < Racers[j].transform.parent.gameObject.transform.GetChild(1).GetComponent().positionHits))*
{
GameObject go = Racers*;*
Racers = Racers[j];
Racers[j] = go;
Racers*.transform.parent.GetComponent().playerPos = Racers.Length - i;*
}

else if ((Racers*.transform.parent.gameObject.transform.GetChild(1).GetComponent().positionHits == Racers[j].transform.parent.gameObject.transform.GetChild(1).GetComponent().positionHits))*
{
if (Racers*.transform.parent.gameObject.GetComponent().nearestDist > Racers[j].transform.parent.gameObject.GetComponent().nearestDist)*
{
GameObject go = Racers*;*
Racers = Racers[j];
Racers[j] = go;
Racers*.transform.parent.GetComponent().playerPos = Racers.Length - j;*
}
else if (Racers*.transform.parent.gameObject.GetComponent().nearestDist < Racers[j].transform.parent.gameObject.GetComponent().nearestDist)*
{
GameObject go = Racers*;*
Racers = Racers[j];
Racers[j] = go;
Racers*.transform.parent.GetComponent().playerPos = Racers.Length - i;*
}
}
}
}
}
}

The first thing that jumps out is that you have a lot of hard coded values in your checkpoint handling. I bit of a structure update could help reduce the amount of reused code.

I would suggest making a GameObject that would store all your checkpoints as child objects, making sure that the checkpoints are in the proper order. Then you could attach a script similar to this:

public class TrackCheckpoints : MonoBehaviour{
	public Transform GetCheckpoint(int index) {
		return transform.GetChild(index);
	}
	
	public int GetNextIndex(int index) {
		int nextIndex = current + 1;
		if (nextIndex >= transform.childCount) {
			return 0;	
		}
		return nextIndex;
	}
	
	public bool IsFinishLine(int index) {
		return transform.childCount - 1 == index;
	}
}

This will allow any Racer check the track to get a specific checkpoint. The ‘GetNextIndex’ method will wrap back to the first checkpoint when given the index of the last checkpoint.

The Racer can use the TrackCheckpoint similar to the following:

public class Racer : MonoBehaviour{
	public int currentLap = 0;
	public int currentIndex = 0;
	public TrackCheckpoints track;
	
	public void Awake() {
		// Get a reference to the Track's Checkpoints
		track = FindObjectOfType<TrackCheckpoints>();	
	}
	
	public float DistToNextCheckpoint() {
		var nextCheckpoint = track.GetCheckpoint(
			track.GetNextIndex(currentIndex));
		
		return Vector3.Distance(
			transform.position,
			nextCheckpoint.transform.position
		);
	}
	
	public void OnTriggerEnter(Collider other) {
		if (other.gameobject.tag == "Checkpoint") {
			if (other.transform == track.GetCheckpoint(currentIndex)) {
				if(track.IsFinishLine(currentIndex)) {
					currentLap++;
					if (currentLap >= 3) {
						// Trigger Racer Finished Here
					}
				}
				currentIndex = track.GetNextIndex(currentIndex);
			}
		}
	}
}

Now the Racer will only have to keep track of the index of their next checkpoint. Each time the Racer hits a checkpoint’s trigger they will check if its the correct one, then update the next checkpoint and update the current lap.

Finally you could update the Positioning class to use the update Racer class:

public class Positioning : MonoBehaviour {
	public List<Racer> Racers { get; private set; }
	
	public void Awake() {
		// Find all the racers in the scene
		Racers = FindObjectsOfType<Racer>();
	}
	
	public void Update() {
		Racers.Sort((r1, r2) => {
			if (r1.currentLap != r2.currentIndex)
				return r1.currentLap.CompareTo(r2.currentLap);
			
			if (r1.currentIndex != r2.currentIndex)
				return r1.currentIndex.CompareTo(r2.currentIndex);
			
			return r1.DistToNextCheckpoint().CompareTo(r2.DistToNextCheckpoint());
		});
		
		// Racers are sorted, you can update positions on racers here if needed.
	}
}

Note: Not sure if there are any errors above. I didn’t have unity on hand so I wasn’t able to test it.