- Home /
Player Race Position
Hi all,
I'm trying to create a small race between some game objects.
When the player's game object (tagged and named as player) runs into the collider, I would like to work out whether the player has come 1st, 2nd or 3rd against their opponents.
I'm just trying to get an idea of how I should go about scripting the event.
Thank you :)
Answer by syclamoth · Oct 31, 2011 at 03:57 AM
You'd do something like this-
List<Player> winners = new List<Player>();
every time a player crosses the finish line, do this-
winners.Add(crossingPlayer);
Then at the end, loop through the 'winners' list, and get the first 3 members!
(You'll need to import System.Collections.Generic for this, btw)
Answer by fabclaudio · Mar 25, 2019 at 09:58 PM
(eng) Hello everyone, I state that I translate with a web translator, being Italian, I have a problem with a RaceManager script, the script is linked to a Car script, they work properly, I just can't seem to extrapolate the positions, everything I managed to do and extrapolate the number of cars present and the name of the first car at the start, please help me I'm getting crazy, I would just need to view the name of the cars in the correct order, or the number of the first player's position, I would prefer the names of the cars, I ask you to add some scripts in the explanation or the script to use, because I am a novice and translating with a web translator it is very difficult for me to understand what you are saying to me, I thank you in advance. (ita) salve a tutti, premetto che traduco con un traduttore web, essendo italiano ,ho un problema con uno script RaceManager, lo script e collegato ad un script Car, essi funzionano correttamente,solo che non riesco ad estrapolarre le posizioni ,tutto cio che sono riuscito a fare e estrapolare il numero di auto presenti e il nome della prima auto allo start, vi prego aiutatemi sto diventando pazzo, a me basterebbe anche solo visualizare il nome delle auto nel ordine corretto ,o il numero della posizione del primo giocatore ,preferirei i nomi delle auto ,vi chiedo di aggiungere qualche script nella spiegazione o lo script da usare, perché sono novizio e traducendo con traduttore web mi è molto difficile capire cosa mi state dicendo, vi ringrazzio in anticipo.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class RaceManager : MonoBehaviour { public Car[] allCars; public Car[] carOrder; public GameObject posizione;
public void Start() { // set up the car objects carOrder = new Car[allCars.Length]; InvokeRepeating("ManualUpdate", 0.5f, 0.5f);
}
// this gets called every frame public void ManualUpdate() { foreach (Car car in allCars) { carOrder[car.GetCarPosition(allCars) - 1] = car;
} posizione.GetComponent().text = "" + allCars.Length; }
}