Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nasapc123 · Aug 25, 2011 at 01:18 PM · playerobjectsclosest

closest 5 or so objects

is it possible to find the closest few objects to the player? if so, how would i go about it? i am using an overlap sphere but cannot seem to limit it to only a few objects...

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AngryMarine · Aug 29, 2011 at 08:54 AM

I have a solution but you need to refit it for your purposes a bit.

       var returnarray = new Array();
       var myStoredReturns = new Array();

       returnarray = Physics.OverlapSphere (explosionPos1, radius);

       var i : int = 0;
       for(var n = 0; n < returnarray.length; n++) {

              if(returnarray[n].gameObject.name == "Warehouse Container Red"){

                      myStoredReturns[i] = returnarray[n]
                      i++;
              }

       }

This code creates two variables i (simple integer) and myStoredReturns which is an array you can store your returns in. Each time the IF flags as true it will add the item to the array and move to the next index.

To limit it just to 5 indexes of myStoredReturns simply do this after the above code.

      for(var p = 0; p < 5; p++) {
            // Do something with each index of myStoredReturns[p] up to 5 indexes.
      }

It's not pretty but its a fairly quick and easy way to accomplish what you are looking for. Don't forget to accept the answer if it answers your question. Thanks!

oh btw: Since overlap sphere returns a Collider, try using returnarray[n].name instead. It should match identically with the name you give the object in the inspector/scene.

Comment
Add comment · Show 5 · 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 AngryMarine · Aug 29, 2011 at 08:55 AM 0
Share

thisDistance and range are simple Float vars. Give it a shot building your list of nearest 5 and if you need help I'll check up on ya.

avatar image nasapc123 · Aug 30, 2011 at 07:10 AM 0
Share

i think its because im an idiot, but i cant manage to make it work... i cant make it limit it to 5, its either everything, or the script wont pick anything. Heres my code:

var returnarray = new Array();

returnarray = Physics.OverlapSphere (explosionPos1, radius);

         for(var n = 0; n < returnarray.length; n++) {

         

         if(returnarray[n].gameObject.name == "Warehouse Container Red"){

             returnarray[n].rigidbody.useGravity = false;

             returnarray[n].transform.RotateAround (transform.position, Vector3.up, rotatetime * Time.deltaTime);

             returnarray[n].transform.position = Vector3.Lerp(returnarray[n].transform.position, transform.position, speedtime);

                     }

                 }
avatar image AngryMarine · Aug 30, 2011 at 07:09 PM 0
Share

Ok well to limit to just the 5 returns I would have the FOR statement like this:

for(var n = 0; n < 5; n++) { //Limits it to the first 5 indexes regardless of what the returns are

Remember also, the string value of "Warehouse Container Red" is case sensative. If you are trying to find 5 Warehouse Container Red's out of all the overlap sphere returns that's a bit different.

To sift through the returns and find only certain returns using Overlap sphere, you might want the if execution a bit different. I've updated the answer to reflect this.

avatar image nasapc123 · Aug 31, 2011 at 12:12 PM 0
Share

Thankyou so so much!!!!! It worked perfectly!!!!

avatar image AngryMarine · Aug 31, 2011 at 06:40 PM 0
Share

Glad to help. Good luck with your game.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

get nearest instance of game object 1 Answer

Player hanging on objects. 0 Answers

Object carry in game 1 Answer

Deactivate/activate objects depending on player position 2 Answers

Player falling too fast for a collider to stop it 5 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