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
0
Question by Heke · Oct 15, 2013 at 06:38 PM · objectnearest

Is this code expensive?

 function Closest(){
     var seeks : GameObject[];
     seeks = GameObject.FindGameObjectsWithTag("Ally");
     var distance = 100;
     var distance2 = 100;
     var position = transform.position;
     var seekOne : GameObject;
     var seekTwo : GameObject;
     var seekThree : GameObject;
     for(var seek : GameObject in seeks){
         var seekDistance = Vector3.Distance(seek.transform.position, position);
         if(seekDistance < distance){
             if(seekOne){
                 var seekOneDistance = Vector3.Distance(seekOne.transform.position, position);
                 if(seekDistance < seekOneDistance){
                     seekTwo = seek;
                 }
                 else if(seekDistance > seekOneDistance){
                     seekTwo = seekOne;
                     seekOne = seek;
                 }
             }
             else{
                 seekOne = seek;
             }
             /*seekThree = seekTwo;
             seekTwo = seekOne;
             seekOne = seek;*/
             //distance = seekDistance;
             
             
         }
     }
     target = seekOne;
     targetTwo = seekTwo;
     targetThree = seekThree;    
 }

Hi! I turned Unity's example code for finding nearest object into code that finds 2 nearest objects.. and it works. My question is, is this code very expensive as it has to be calculated every frame? I don't quite know yet what makes the code more expensive? Are if statements one of those?

Edit: pic alt text

seekhundredth.png (34.6 kB)
Comment
Add comment · Show 1
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 Tomer-Barkan · Oct 15, 2013 at 07:05 PM 0
Share

Share the code so we can help you without going and installing unity sample projects.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by flaviusxvii · Oct 15, 2013 at 06:50 PM

You didn't post any code...

But"nearest" naturally involves distance, and finding the distance between two points is generally expensive. Since you don't need the actual distance, and just the "relative distances" you can save some time using

http://docs.unity3d.com/Documentation/ScriptReference/Vector3-sqrMagnitude.html

There are tons of other optimizations you can do. But that's the best place to start. It will all depend on how many objects you need to check.. any more than 20 or 30 and you'd probably benefit from using some kind of space partitioning, or using the collision detection in Unity to quickly grab all of the nearby candidates.

Comment
Add comment · Show 2 · 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 Heke · Oct 15, 2013 at 07:14 PM 0
Share

Stupid me, added code with edit.

avatar image flaviusxvii · Oct 15, 2013 at 08:24 PM 0
Share

Sine you have to check the distance to every object, you might as well do that first, and then make decisions about the objects later..

 import System.Collections.Generic;
 
 
 var sortedByDistance = new SortedDictionary.<float, GameObject>();
 
 for(var seek : GameObject in seeks) {
     var distance = (seek.transform.position - position).sqr$$anonymous$$agnitude;
     sortedByDistance.Add(distance, seek); 
 }
 
 var count : int = 0;
 for(var distance in sortedByDistance.$$anonymous$$eys) {
     count++;
     if(count == 1) {
         seekOne = sortedByDistance[distance];
             continue;
     }
     if(count == 2) {
         seekTwo = sortedByDistance[distance];
             continue;
     }
 
     break;
 }

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

16 People are following this question.

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

Related Questions

How could I calculate the distance to recieve the nearest object to the player? 2 Answers

Find nearest object using Physics.OverlapSphere 2 Answers

Get nearest object and find direction problem 2 Answers

Physics.OverlapSphere to get 5 Nearest objects 0 Answers

Manipulating Instanced Object 2 Answers


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