• I make a new script that attached in Player's Kart or AI Karts. Named Rankers.JS
• Rankers.JS has distance to waypoint variable which is used for Waypoint.js to sort the distance and then return it to the Rankers.JS if they had same distance to waypoint value
This is the screencap


The problem on this script is
- Works perfectly on Lap1, But it was broken on Lap 2
- They still think they are #1 even they still lap 1 meanwhile other player is lap 2
- Duplicated avatar. Wrong character position displayed
- this code i attached is working perfectly but it has a problem on lap 2.
And this is my code
// RANKER.JS SCRIPT THAT ATTACHED IN KART.
#pragma strict
var currentRank = 1; //This should return to GM to display the current position (like #1, or #2)
var isPlayer = false; //Only turn on if this is player's kart
var distanceToWaypoint : float;
private var player : Kart;
private var aiCar : AIKart;
private var gm : GM;
private var crg : CoreGUI;
function Start () {// Invoked when the Engine starts
if(isPlayer) player = GetComponent(Kart);
else aiCar = GetComponent(AIKart);
gm = GameObject.FindObjectOfType(GM);
if(GameObject.FindObjectOfType(CoreGUI))crg = GameObject.FindObjectOfType(CoreGUI);
}
function Update () {
if(currentRank >= 8) currentRank = 8; // Don't let it can be more than 8
if(currentRank <= 1) currentRank = 1;
if(isPlayer){
player.currentRank = currentRank; // sets to the Player's kart script then send it again to GM to check the position
distanceToWaypoint = Vector3.Distance(transform.position,player.nextWaypoint.transform.position);
player.nextWaypoint.playerDistances[player.carId] = distanceToWaypoint;
gm.UpdateInfo(player.carId,player.transform,distanceToWaypoint); // Update ranking UpdateInfo(ID : int, playerKart : Transform, distance : float)
if(crg && currentRank <=4) crg.UpdateRankData(player.carId,currentRank); // Update displayed rank if this player rank is less than 4
for(var plydsc = 0; plydsc < player.nextWaypoint.distanceSorter.length; plydsc++){
//The main component that make this thing works.
if(distanceToWaypoint == player.nextWaypoint.distanceSorter[plydsc]) currentRank = plydsc;
}
}else{ // if this is not player then this is AI
aiCar.currentRank = currentRank; //Same with above, but this just for AI
distanceToWaypoint = Vector3.Distance(transform.position,aiCar.waypointContainer.transform.position);
aiCar.waypointContainer.playerDistances[aiCar.carId] = distanceToWaypoint;
gm.UpdateInfo(aiCar.carId,aiCar.transform,distanceToWaypoint);
if(crg && currentRank <= 4) crg.UpdateRankData(aiCar.carId,currentRank);
for(var aidsc = 0; aidsc < aiCar.waypointContainer.distanceSorter.length; aidsc++){
if(aiCar.waypointContainer.distanceSorter[aidsc] == distanceToWaypoint) currentRank = aidsc;
}
}
}
// WAYPOINT.JS THE RANK CHECKER
//#pragma strict
var karts : Array; // Not used, useless
var gizmoColor : Color;
var next : Transform; // Where's the next waypoint ?
var sfx : AudioClip[]; //Sound effect to play when the player hits finish line
var whoAlreadyEntered = new boolean[18]; //Check if the player has entered or not so the player can't cheated
var enteringStatus : boolean[]; //same with above, different method
var playerDistances = new float[18]; //All player's distances
var distanceSorter = new float[8]; // Sorted Rankings
var playerLaps = new int[18]; //All player's lap
var waypointLength = 14; // How much waypoint we got there ?
var isStart = false; // Is this a start ? If it is, then player's lap will increase everytime you hit
private var player : Kart;
private var audioSrc : AudioSource;
private var counter = 0; //not used
function Start(){
if(!GameObject.Find("AS")){
var zaudioSrc = new GameObject("AS");
zaudioSrc.AddComponent(AudioSource);
audioSrc = zaudioSrc.audio;
}else audioSrc = GameObject.Find("AS").audio;
player = GameObject.FindObjectOfType(Kart);
karts = GameObject.FindObjectsOfType(AIKart);
karts.Add(player);
enteringStatus = new boolean[karts.length];
//Destroy(this);
}
function Update(){
// RANK CHECKER
// THE MAIN COMPONENT OF RANK CHECKER
distanceSorter[0] = playerDistances[0];
distanceSorter[1] = playerDistances[1];
distanceSorter[2] = playerDistances[2];
distanceSorter[3] = playerDistances[3];
distanceSorter[4] = playerDistances[4];
distanceSorter[5] = playerDistances[5];
distanceSorter[6] = playerDistances[6];
distanceSorter[7] = playerDistances[7];
distanceSorter[8] = playerDistances[8];
for(var dstx in distanceSorter) if(dstx == 0) dstx = 100000; //there's shouldn't be 0 value in distance. Zero value isn't number 1 move it to last position!
distanceSorter.Sort(distanceSorter,1,8); //Sort the distances from distanceSorter[1] to [8] then return the index to ranker.js
}
function OnTriggerEnter(col : Collider){
if(col.gameObject.tag == "Opponent"){
var aiz = col.transform.root.GetComponent(AIKart);
aiz.GotNewWaypoint(next.position,next.rotation,next.GetComponent(Waypoint));
if(isStart){
if(aiz.currentWaypoint == waypointLength){
// CLEAR The waypoint data, so the player can enter the waypoint again.
ClearWaypointData();
aiz.currentWaypoint = 0;
aiz.currentLap ++;
aiz.currentPosition = Vector3(next.position.x,aiz.transform.position.y,next.position.z);
aiz.currentRotation = next.rotation; // Saves the rotation and position so player will respawn here everytime they got errors.
}
}else{
if(!whoAlreadyEntered[aiz.carId]){ //If i didn't enter this waypoint yet, go ahead
whoAlreadyEntered[aiz.carId] = true;
aiz.currentWaypoint ++;
aiz.currentPosition = Vector3(transform.position.x,aiz.transform.position.y,transform.position.z);
aiz.currentRotation = transform.rotation;
}
}
}
if(col.gameObject.tag == "Player"){
player.currentPosition = Vector3(transform.position.x,player.transform.position.y,transform.position.z);
player.currentRotation = transform.rotation;
player.GotNewWaypoint(next.GetComponent(Waypoint));
//same with above, but this is for Player
if(isStart){
if(player.currentWaypoint == waypointLength){
//enteringStatus[counter] = true;
//counter++;
ClearWaypointData();
audioSrc.PlayOneShot(sfx[0]);
player.currentWaypoint = 0;
player.currentLap ++;
}
}else{
if(!whoAlreadyEntered[player.carId]){
player.currentWaypoint ++;
whoAlreadyEntered[player.carId] = true;
}
}
}
}
function ClearWaypointData(){
var xwp = GameObject.FindObjectsOfType(Waypoint);
for(var xwps in xwp) {
for(var xxx in xwps.whoAlreadyEntered) xxx = false;
if(!xwps.isStart)for(var xmp in xwps.playerDistances) xmp = 0; // Reset all waypoint data
}
}
So please, help me. I just want to make it correct position, and no problem on lap 2.
Thanks for any help.

New Topic/Question
Reply



MultiQuote






|