Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
Aeleas

Sort first objects nearest me ?

Question

Hi.

I need to make my object read the first 3 objects that close to him, and sort it by the distance. The first in the array is the nearest one...and so on.  After that, find a script inside those in the list to active the bool option "enemy_checker".

Please login or register to see this code.

 

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You right. List is more flexible for this.

Thanks god that I found some one give me good solution. Here is how he solve it.

Please login or register to see this code.

 

Share this post


Link to post
Share on other sites

  • 0

Hi. I recommend you to use a list which you could easily convert to an array. A few months ago I had written an old post about a similar request - it seems to me that it is exactly what you are trying to do. Take a look at the function sortArray which call another one - checkDistance - so as to sort in a list all objects - nearest to farthest.  Then the list is converted to an array. I see in your previous code that you know how to use a simple loop - so you can interact with the three first objects in your array. I hope that it could be useful to you ++

 

Please login or register to see this link.

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

Good to see you got it going. Friendly advice tho, change enemiesInRange from a list of gameObjects to a list of enemyPath. All those get components eat up a lot of rss bc your using it an update loop. So just cache the component on trigger enter and you don’t have to get it up to 3x, 60 times a second.

 

edit: and whenever you need to transform or go, you would just call enemiesInRange[index].transform (or gameObject) since the component caches the transform/gameObject that it’s attached to

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...