Hello, I’m making a side scrolling(from the left to the right) racing game and I’m trying to calculate the player’s position. But it’s really messed up, It sometimes only calculates for 1 car and other times for both. This is the code I’ve got so far
if (!hasFinishedRace)
{
foreach (Transform t in aiCars)
{
print(t.name);
if (player.position.x > t.transform.position.x)
{
if (playerPosition > 1)
{
playerPosition--;
}
}
else
{
if (playerPosition < aiCars.Length + 1)
{
playerPosition++;
}
}
}
if (playerPosition == 1)
{
positionText.text = playerPosition.ToString() + "st";
}
else if (playerPosition == 2)
{
positionText.text = playerPosition.ToString() + "nd";
}
else if (playerPosition == 3)
{
positionText.text = playerPosition.ToString() + "rd";
}
if (playerPosition != 3 && playerPosition != 2 && playerPosition != 1)
{
positionText.text = playerPosition.ToString() + "th";
}
}