- Home /
ranking in race Game
This is my IA Script about my Car but i need to add the rank or position of my car in race how to do it:
var CenterDeGravite :Vector3;
var Chemin :Array ;
var CheminGroupe :Transform;
var MaxAngle :float =15.0;
var RavG :WheelCollider;
var RavD :WheelCollider;
var RarG :WheelCollider;
var RarD :WheelCollider;
var Chemin_obj_Actuelle :int;
var Distanc_Depuis_Chemin :float=20;
var MaxMoteur :float =50;
var VitesseActuelle :float;
var TopVitesse :float=150;
var DecellerationVitesse :float =10;
var ArriereLightD : GameObject;
var ArriereLightG : GameObject;
var FreinMain : boolean =false;
var frienmech : Material ;
var danssecteur : boolean ;
var longueurcapteur : float = 5;
var avantcapteurdebut : float = 5 ;
var avantcapteurdistance : float = 5;
var avantcapteurangle : float = 30 ;
var laterlacapteurlongeur : float = 5 ;
private var flag : int = 0 ;
var eviter_sensibilite : float = 0 ;
function Start () {
rigidbody.centerOfMass = CenterDeGravite;
GetChemin();
}
function GetChemin(){
var Chemin_objs :Array = CheminGroupe.GetComponentsInChildren(Transform);
Chemin = new Array();
for(var Chemin_obj : Transform in Chemin_objs){
if(Chemin_obj != CheminGroupe)
Chemin [Chemin.length]= Chemin_obj;
}
}
function Update () {
MarcheArriereLight ();
GetAngle();
Mouvement();
capter();
}
function GetAngle(){
var AngleVector :Vector3 =transform.InverseTransformPoint(Vector3(Chemin[Chemin_obj_Actuelle].position.x,transform.position.y,Chemin[Chemin_obj_Actuelle].position.z));
var NouveauAngle :float = MaxAngle *(AngleVector.x / AngleVector.magnitude);
RavG.steerAngle =NouveauAngle;
RavD.steerAngle =NouveauAngle;
if(AngleVector.magnitude <= Distanc_Depuis_Chemin) {
Chemin_obj_Actuelle++;
if( Chemin_obj_Actuelle >= Chemin.length){
Chemin_obj_Actuelle=0;
}
}
}
function Mouvement(){
VitesseActuelle = 2*(22/7)* RarG.radius * RarG.rpm *60 /1000 ;
VitesseActuelle = Mathf.Round(VitesseActuelle);
if(VitesseActuelle <= TopVitesse && !danssecteur) {
RarG.motorTorque= MaxMoteur;
RarD.motorTorque= MaxMoteur;
RarG.brakeTorque=0;
RarD.brakeTorque=0;
}
else if (!danssecteur) {
RarG.motorTorque=0;
RarD.motorTorque=0;
RarG.brakeTorque=DecellerationVitesse;
RarD.brakeTorque=DecellerationVitesse;
}
}
function MarcheArriereLight () {
if (VitesseActuelle <0 && FreinMain ){
ArriereLightG.GetComponent(Light).enabled = true ;
ArriereLightD.GetComponent(Light).enabled = true ;
ArriereLightD.light.color = Color.red;
ArriereLightG.light.color = Color.red;
}
else if (VitesseActuelle <0 && !FreinMain ) {
ArriereLightG.GetComponent(Light).enabled = true ;
ArriereLightD.GetComponent(Light).enabled = true ;
ArriereLightD.light.color = Color.white;
ArriereLightG.light.color = Color.white;
}
else if ( !FreinMain ) {
ArriereLightG.GetComponent(Light).enabled = false ;
ArriereLightD.GetComponent(Light).enabled = false;
}
}
function capter() {
flag=0;
var pos : Vector3 ;
var hit : RaycastHit ;
var angledroit = Quaternion.AngleAxis(avantcapteurangle,transform.up)*transform.forward;
var anglegauche = Quaternion.AngleAxis(-avantcapteurangle,transform.up)*transform.forward;
// avant milieu capteur
pos = transform.position ;
pos += transform.forward*avantcapteurdebut;
if (Physics.Raycast(pos,transform.forward,hit,longueurcapteur)){
Debug.DrawLine(pos,hit.point,Color.yellow);
}
// avant capteur droit
pos += transform.right*avantcapteurdistance;
if (Physics.Raycast(pos,transform.forward,hit,longueurcapteur)){
if (hit.transform.tag != "terrain"){
eviter_sensibilite -=0.5 ;
Debug.Log("lool");
flag++;
Debug.DrawLine(pos,hit.point,Color.yellow);
}
}
// anvant capteur angle droit
if (Physics.Raycast(pos,angledroit ,hit,longueurcapteur)){
if (hit.transform.tag != "terrain"){
Debug.DrawLine(pos,hit.point,Color.yellow);
transform.Rotate(Vector3.up,- 90 * 2* Time.smoothDeltaTime);
}}
//avant capteur gauche
pos = transform.position ;
pos += transform.forward*avantcapteurdebut;
pos -= transform.right*avantcapteurdistance;
if (Physics.Raycast(pos,transform.forward,hit,longueurcapteur)){
if (hit.transform.tag != "terrain"){
eviter_sensibilite +=0.5 ;
Debug.Log("lool");
flag++;
Debug.DrawLine(pos,hit.point,Color.yellow);
}
}
//avant capteur angle gauche
if (Physics.Raycast(pos,anglegauche,hit,longueurcapteur)){
if (hit.transform.tag != "terrain"){
Debug.DrawLine(pos,hit.point,Color.yellow);
transform.Rotate(Vector3.up, 90 * 2* Time.smoothDeltaTime);
}
}
//droit lateral capteur
if (Physics.Raycast(transform.position ,transform.right,hit,laterlacapteurlongeur)){
Debug.DrawLine(transform.position,hit.point,Color.yellow);
}
// gauche lateral capteur
if (Physics.Raycast(transform.position ,-transform.right,hit,laterlacapteurlongeur)){
Debug.DrawLine(transform.position,hit.point,Color.yellow);
}
if (flag !=0)
{eviter_angle (eviter_sensibilite);}
}
function eviter_angle(sensibilite : float)
{
RavG.steerAngle =MaxAngle*sensibilite;
RavD.steerAngle =MaxAngle*sensibilite;
}
That's a lot of code to go through and very little description about what you'd like help with. Could you cut it down to what is relevant and maybe explain your issue further?
Answer by fafase · May 02, 2014 at 07:29 AM
As mentioned in the comment, that is way to much code to look at, so I won't read it and just give you some ideas on how we made it in one of our car game.
Each car starts with a value of 0 for points.
You have waypoints, which I reckon is CheminGroupe in your script. When a car passes the waypoint, points gets increased.
Ordering the cars by points then gives a first ranking.
But cars in between the same waypoint will show with same ranking or a random ranking more likely. This is where you take the distance to the next waypoint.
So first rank by points and then if some have the same amount of points rank by distance.
I would suggest to create a method that does all that then call the method via
InvokeRepeating("CarOrder",0.5f,0.5f);
Then it won't happen each frame and half a second is enough to get accurate ranking.
Answer by PointyPigheadGames · Oct 19, 2016 at 09:58 PM
I'm not going to give the exact code so i'll tell you what you need to do.
In this order:
Are the other cars on the same lap that you are?
If so, are the other cars on the same checkpoint/waypoint as you are?
If so, are the other cars a greater distance from the checkpoint/waypoint than you are?
Next, you make an int variable that will be a 0 if the car is ahead of you, and a 1 if the car is in front of you. Finally, make another int that is 1+ car1Position + car2Position. This will be the place you're in.
This should help make an accurate ranking in a race, as long as there are enough checkpoints/waypoints.
Hope this helps! -Pointy Pighead Games
Your answer
![](https://koobas.hobune.stream/wayback/20220613144052im_/https://answers.unity.com/themes/thub/images/avi.jpg)