Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by serialdrifter · Jan 31, 2012 at 01:23 AM · arraysdistancevaluesracing

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]);
      }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 31, 2012 at 01:29 AM 0
Share

Before I go digging into your code (it looks pretty good, btw), could you explain exactly what your problem is?

avatar image serialdrifter · Jan 31, 2012 at 01:35 AM 0
Share

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!

avatar image syclamoth · Jan 31, 2012 at 01:39 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image serialdrifter · Jan 31, 2012 at 02:10 AM 0
Share

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");
  }

}

avatar image syclamoth · Jan 31, 2012 at 02:14 AM 0
Share

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.

avatar image serialdrifter · Jan 31, 2012 at 02:43 AM 0
Share

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 :(

avatar image syclamoth · Jan 31, 2012 at 02:56 AM 0
Share

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'.

avatar image serialdrifter · Jan 31, 2012 at 03:06 AM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges