5 Replies - 1633 Views - Last Post: 14 March 2013 - 09:40 AM Rate Topic: -----

#1 tntcenturion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

[Unity3D] Problem on my Racer Position Script.

Posted 13 March 2013 - 03:05 AM

Sorry, i still asking here. But it's kinda difficult to solve it. So this is the Method i use :
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
Posted Image
Posted Image

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.

Is This A Good Question/Topic? 0
  • +

Replies To: [Unity3D] Problem on my Racer Position Script.

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Unity3D] Problem on my Racer Position Script.

Posted 13 March 2013 - 12:49 PM

I haven't really read through your entire code because there is a lot going on in there. I do feel that the code can be simplified a great deal - which is the reason for your incorrect rankings. As an example the current lap should simply be the number of times that the start/finish line is crossed. To determine who finish is timed based on who completed the number of laps quickest.

Race rankings during the race? I think I've given you enough clues to work that one out! :)
Was This Post Helpful? 0
  • +
  • -

#3 tntcenturion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

Re: [Unity3D] Problem on my Racer Position Script.

Posted 13 March 2013 - 11:41 PM

View PostButchDean, on 13 March 2013 - 12:49 PM, said:

I haven't really read through your entire code because there is a lot going on in there. I do feel that the code can be simplified a great deal - which is the reason for your incorrect rankings. As an example the current lap should simply be the number of times that the start/finish line is crossed. To determine who finish is timed based on who completed the number of laps quickest.

Race rankings during the race? I think I've given you enough clues to work that one out! :)/>


Well.. about the problem in lap 2 is fixed. Only 1 problem left.
  • Me (Player) is now Lap 2. And it still shows correct position #1.
  • Then suddenly there's an AI in front of me, and he's still lap 1. But the GM says I'm #8 meanwhile he's #1

Do you know how to make it displays correct position with correct lap. It's unfair, I'm lap 2 and he's just lap 1 but the GM says he's #1.

You might ask why this happens. Well, it happens because waypoint still calculating player's distances. After all player hits the waypoint, the waypoint will stop calculating player's distance and back works normally. I know the problem, but i don't know how to fix it.

This is the code that controls its rank (Simplified)

//Attached in player's kart
for(var plydsc = 0; plydsc < player.nextWaypoint.distanceSorter.length; plydsc++){
	//Checks the waypoint sorted distances. Returns the index of sorted distance if they have same distance value.
	/*Instance 

	distanceToWaypoint : float. Sends it to waypoint player distances. Then Waypoint send back sorted distance to this code.
	in this script, check the waypoint sorted distance. Which array index has same value with this distanceToWaypoint
	then plydsc is sorted distance index. 

	1. distanceToWaypoint = 300
	2. distanceSorter[1] = 200, distanceSorter[2] = 300, distanceSorter[3] = 400, etc.
	3. oh look there's a same value distanceSorter[2] and distanceToWaypoint, i better returns the index (plydsc)
	4. Oh, now i'm #2

	*/
	if(distanceToWaypoint == player.nextWaypoint.distanceSorter[plydsc]) currentRank = plydsc;
	}



So do you know how to make a lap exception (like i was said before). Thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Unity3D] Problem on my Racer Position Script.

Posted 14 March 2013 - 04:13 AM

Why is your distanceToWaypoint a magic number (300)? Where does 300 come from? It should be calculated.

Also, you don't need to calculate the distance to the waypoint anyway, you need to calculate the distance between cars on the same lap in order to determine position.
Was This Post Helpful? 0
  • +
  • -

#5 tntcenturion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

Re: [Unity3D] Problem on my Racer Position Script.

Posted 14 March 2013 - 07:46 AM

View PostButchDean, on 14 March 2013 - 04:13 AM, said:

Why is your distanceToWaypoint a magic number (300)? Where does 300 come from? It should be calculated.

Also, you don't need to calculate the distance to the waypoint anyway, you need to calculate the distance between cars on the same lap in order to determine position.


distanceToWaypoint is calculated by using Vector3.Distance(transform.position,nextWaypoint.position); i just make an example value up there.

You think it's a good idea, making a race rank manager by calculating distance between cars ? But i already tried this method. I think it has same problem. Still can't fix the Lap. I use same method with this waypoint method. Find the same value between ranker.js and waypoint.js, then return the array index of sortedDistance as ranking.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Unity3D] Problem on my Racer Position Script.

Posted 14 March 2013 - 09:40 AM

What the problem appears to be here (or maybe one of them), is that you are using affine transformation calculations to work out race positions on a track with curves. This will not work reliably.

You need triggers on the track that report track positions for the respective cars. It's going to be a bit if work but it's simple and reliable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1