- Home /
Comparing values of an array
Hello,
After alot of searching i just can't seem to find a solution to this problem i'm having.
I have an array which holds the distances of the playercar and the 3 ai cars to the finishline. How would i go and compare these values, and making a guitext show the players position.
I'm still learning hard, so maybe i'm in way over my head, but it just seems that i'm so close now, but it's still missing a small vital part :(
Any help is really appreciated, and here is what i have so far:
var cars : Transform[];
var finish : Transform;
var maxDistance = 0;
var minDistance = 10000;
var distance : float[];
function Update () {
for(i=0; i<4; i++){
distance[i] = (finish.position - cars[i].position).magnitude;
if(distance[i] < minDistance)
minDistance = distance[i];
if(distance[i] > maxDistance)
maxDistance = distance[i];
}
}
function OnGUI(){
for(i=0; i<4; i++){
GUILayout.Label("Distance " + i + ": " + distance[i]);
}
}
Before I go digging into your code (it looks pretty good, btw), could you explain exactly what your problem is?
Well, all i have now are the 4 distances of the 4 cars to the finish. If the players distance is smaller then the ai1 distance, but bigger then the ai2 distance, the player would be in second position.
Now, how do i make a GUIText(or something else, doesnt really matter) display in which position the player is in, by comparing these distance values?
Thanks in advance!
Ah, I see! Well now, all you need to do here is sort the cars by distance. I'll go into a bit more detail in an actual answer.
Answer by syclamoth · Jan 31, 2012 at 01:53 AM
First up, put the line
import System.Collections.Generic;
at the top, to make sure that you have all the classes that you'll need for this.
Now, make a variable called
var sortedCars : SortedList.<Float, Transform>;
This is what you will use to keep track of the cars' positions.
In your Update function, use this:
sortedCars = new SortedList.<Float, Transform>();
for(var carTrans : Transform in cars)
{
var carDistance : float = (finish.position - carTrans.position).magnitude;
sortedCars.Add(float, carTrans);
}
Now, having done that, you can use this list in your OnGUI function to draw a list of cars, by their distance from the finish line!
var i : int = 0;
for(var curPosition : KeyValuePair.<Float, Transform> in sortedCars)
{
GUILayout.Label(i + ": " + curPosition.Value.gameObject.name + ", " + curPosition.Key + " metres from finish");
}
I think that should do it, although I admit that I'm not too familiar with generics in UnityScript. In fact, I wasn't even aware that they existed until quite recently! If there are any errors, just tell me and I'll try to fix up my answer.
First of all, thanks for the explanation. I tried building it however, and it gives me a error:
"The best overload for the method 'System.Collections.Generic.SortedList. .Add(float, UnityEngine.Transform)' is not compatible with the argument list '(System.Type, UnityEngine.Transform)'"
here's my current code, after changing it:
import System.Collections.Generic;
var sortedCars : SortedList. ; var cars : Transform[]; var finish : Transform; var maxDistance = 0; var $$anonymous$$Distance = 10000; var distance : float[];
function Update () {
for(i=0; i<4; i++) { distance[i] = (finish.position - cars[i].position).magnitude; if(distance[i] < $$anonymous$$Distance) $$anonymous$$Distance = distance[i]; if(distance[i] > maxDistance) maxDistance = distance[i]; }
sortedCars = new SortedList.<float, Transform>();
for(var carTrans : Transform in cars)
{ var carDistance : float = (finish.position - cars[i].position).magnitude; sortedCars.Add(float, carTrans); } }
function OnGUI(){
var i : int = 0;
for(var curPosition : $$anonymous$$eyValuePair.<float, Transform> in sortedCars)
{
GUILayout.Label(i + ": " + curPosition.Value.gameObject.name + ", " + curPosition.$$anonymous$$ey + " metres from finish");
}
}
O$$anonymous$$. It's a problem with the designation 'float' in Unityscript. See if using 'Float' ins$$anonymous$$d fixes it.
Oh, and there was one other problem with my answer. I'm fixing it now, too.
Changing it to Float didn't help, it says "unknown identifier" then.
The only error in the script is the one in the line "sortedCars.Add(float, carTrans);" definetely something with the float.
The error is still the same :(
Odd that it thinks that it should be of type 'System.Type' ins$$anonymous$$d of an actual float. It works for the 'Transform' bit... try 'System.Float' ins$$anonymous$$d, see if that does it.
EDIT: also try System.Single, and just 'single'.
Nope :( "Language feature not implemented: System."
That's what i get after changing it to "sortedCars.Add(System.Float, carTrans);"
EDIT; tried using both of these, not working either. I really don't have a clue about what's causing this. Does this code snippet you posted work with you? $$anonymous$$aybe there is something else conflicting or something.
Thank you very much for your time btw!
Your answer
Follow this Question
Related Questions
An Array within array 2 Answers
Track players position in a race 0 Answers
Distance measured showing negative values. 1 Answer
Odometer for a racing game 2 Answers
How to check if one of value is true on an array of objects? 1 Answer