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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by headkitgames · Mar 18, 2011 at 05:06 PM · detectneighbour

Detect nearest neighbours (and their neighbours...)

hi there!

i want to let a gameobject find out which other gameobjects of same kind are close to him (distance equals own width/height) and then let these GO's find other GO's of same kind, which are that close (like in a bubble game when a red bubble looks if another red one is near).

what is the best approach here? thnx!!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by Bampf · Mar 18, 2011 at 05:46 PM

One way that should work for many kinds of games would be to use Unity colliders to detect other objects that are nearby. Specifically, try Physics.OverlapSphere to find all colliders in the scene that fall within a sphere centered on the object that you want to find the neighbors of.

Each game object would then store a list of its neighbors. Depending on the game, you might need to update these lists frequently, or not at all.

Will this scheme work for all games? No. There are many ways to do it, and how you do it depends on the situation. But Unity's physics engine is already doing lots of tricks to detect collisions quickly. So using it to find the neighbors should perform well even if you have a lot of objects in your scene.

A separate question is how to store the answers. This depends again on the game and what you want to do with the information. A simple approach is for each object to store a list of references, the list of all its neighboring GameObjects. But you might need to do it differently, for example keep separate lists of red and blue neighbors, or you might need to sort the list by distance so that the closest objects are always first.

Comment
Add comment · 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
0
Wiki

Answer by e-bonneville · Mar 18, 2011 at 07:34 PM

If you're working with a grid, you have another option: Hashtables.

Hashtables have proven invaluable to me many a time when I wanted the tiles in a grid to know their neighbors. What you do is this (psuedocode):

  • When generating the grid, store each the Transform of each gameObject you instantiate in a hashtable, using the position as the key (something like transform.position.ToString()).
  • After you've finished instantiating all your objects (it's crucial to wait to the end), loop through your hashtable, which should have a reference to all the tiles in the scene.
  • For each entry, get the Transforms of the objects around it and store them in either a hashtable or an array on the gameObject. You can then access its neighbors by looking at this hashtable.

To iterate through a hashtable, you can use this code:

foreach(DictionaryEntry currentEntry in hashtableName) {
    string currentEntryKey = currentEntry.Key;
    Transform currentEntryTransform = hashtableName[currentEntry.Key];
}

That's just some useful code for traversing hashtables. (Note that using Transform is not necessary; you can change it to reflect whatever variables you want to store in the hashtable) My answer may or may not be completely unrelated to your question, but it's useful for referencing if you ever have to know the neighbors of an object in a grid. :)

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 e-bonneville · Mar 18, 2011 at 07:39 PM 0
Share

Why's my answer a community wiki? Is it because the question's a community wiki? :/

avatar image headkitgames · Mar 21, 2011 at 10:38 AM 0
Share

yes. I clicked the wiki by mistake and then tried to unlink the wiki again but this seems to be no option.

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

No one has followed this question yet.

Related Questions

How to detect Neighbours? 1 Answer

Detecting the Edge of the Screen 2 Answers

Detecting Nvidia or AMD card in shader 0 Answers

Detection where light hits using SphereCast 1 Answer

Enemy Detection Problem in Pacxon Similar Game 0 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