- Home /
Check points for racing game
Hi! Im creating a racing game in unity and for car positions(e.g Pos:2/8) and lap systems one of my friends gave this piece of code to me....but it counts from 0 to 8... u know... when you are in the last position it writes Pos:0/8 instead of 8/8 and when you pass all of the cars it writes 8/8 instead of 1/8 and the code is here: using UnityEngine; using System.Collections; using System.Collections.Generic;
using System.Linq;
public class CheckpointManager : MonoBehaviour {
public List<GameObject> EnemyCars = new List<GameObject>();
public TextMesh PosText;
void Start()
{
GameObject pLayer = GameObject.FindWithTag("Player");
EnemyCars.Add(pLayer);
}
void Update()
{
GameObject _Player;
int _PlayerPos = 0;
for(int i=0; i<EnemyCars.Count; i++)
{
if(EnemyCars[i].name == "Player")
{
_Player = EnemyCars[i];
_PlayerPos = EnemyCars.IndexOf(EnemyCars[i]);
}
}
EnemyCars.Sort(SortByPassedCheckpoints);
PosText.text = "Pos: " + _PlayerPos.ToString() + "/" + EnemyCars.Count.ToString();
}
int SortByPassedCheckpoints(GameObject C1, GameObject C2)
{
return C1.GetComponent<CheckpointTrigger>().PassedCheckpoints.CompareTo(C2.GetComponent<CheckpointTrigger>().PassedCheckpoints);
}
}
Please help what should i do...thanx
Hi @Ar$$anonymous$$yazdian
You could just change the loop as below
int _PlayerPos = 0;
for(int i=8; i>=EnemyCars.Count; i--)
{
if(EnemyCars[i].name == "Player")
{
_Player = EnemyCars[i];
_PlayerPos = EnemyCars.IndexOf(EnemyCars[i]);
}
}
I think this would work. Please check and ask for further help
Answer by 5c4r3cr0w · Jul 27, 2016 at 11:13 AM
For instant solution Replace :
PosText.text = "Pos: " + _PlayerPos.ToString() + "/" + EnemyCars.Count.ToString();
With
PosText.text = "Pos: " + (EnemyCars.Count + 1 -_PlayerPos).ToString() + "/" + EnemyCars.Count.ToString();
Answer by Arminyazdian · Jul 28, 2016 at 01:19 PM
Sorry guys but it didnt work...thank you so much for your answers @Rajeesh_AR @5c4r3cr0w
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Turbo Functionality 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
C# car script 0 Answers