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
5
Question by · Sep 23, 2010 at 04:51 AM · arraygridneighbouradjacent

Finding adjacent tiles in a grid

I have a 2D grid of tiles, and I need to build an array storing each adjacent tile of any given tile as a once-off operation. i.e. At startup, the adjacent neighbouring tiles for each tile are stored to an array.

  • The size of the tiles is known (1x1), therefore the distance between tile origins is also known.
  • Only want to find adjacent (up/down/left/right) tiles - not diagonal.

So far, there seem to be 3 obvious approaches:

  1. store all tiles to array, iterate through array to distance check with current tile position
  2. overlapsphere around position of tile, distance check (to ignore diagonals)
  3. +/- 1 to x/y of tile, small overlapsphere around the calculated position

At this stage, I'm thinking the third may be the best option, but I wanted to make sure I hadn't overlooked a better approach.

Any feedback or personal experience is welcomed and appreciated!

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
9
Best Answer

Answer by Jesse Anders · Sep 23, 2010 at 05:26 AM

I assume the tiles have been created in the scene just like any other game object, which is why they're not in an array to begin with, correct? I also assume they have colliders - do they need colliders for any purpose other than finding the neighboring tiles?

As far as physics-based approaches go, I would imagine any method that would take advantage of the physics system's broad-phase culling would be fairly efficient. This would include options 2 and 3 in your list, and would also include casting a vertical ray at x/y +/- 1.

That said, the tiles' positions in the array are implicit in their xy coordinates, so another option would be to acquire references to all of the tile objects (e.g. by name, tag, or type), place them in a 2-d array based on their xy coordinates, and then compute the neighbors as you would normally. (Of course if the array would be useful for other purposes as well, you could just keep the array around and dispense with the per-tile neighbor lists entirely.)

Not sure if that's helpful at all, but maybe it'll at least provide another option to consider.

[Edit: There are other options as well, of course; one would be to create a custom editor using editor scripting, and have the editor handle the details of creating the tile array or establishing connectivity between neighboring tiles.]

Comment
Add comment · Show 3 · 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 · Sep 23, 2010 at 07:24 AM 0
Share

Yes, at this stage (prototyping), the grids (levels) have been pre-built and already exist in the scene. They have colliders, but there's no reason that they need them (yet). I mostly wanted to make sure that I was on the right track, and the thinking behind using an overlapshere was sound. Your feedback's much appreciated - upvoted your answer, but will leave the question open for a little longer to encourage other insight. Re: custom editor: I haven't dabbled in editor script, but I do plan to - any resources you'd recommend? Cheers!

avatar image Jesse Anders · Sep 23, 2010 at 10:14 AM 0
Share

I'm just finishing up a custom editor for my project, but to be honest it was a bit of a challenge (due to sparse documentation and various gotcha's I encountered along the way). The resources I found most useful were the 'Extending the Editor' page on the Unity site, the docs (although again a bit sparse), the 'Editor' section of the scripts Wiki, and the forums and this site (I did a lot of digging through previous posts looking for information).

avatar image · Sep 23, 2010 at 03:19 PM 0
Share

The 3rd approach was implemented successfully/easily, and there's no noticeable lag on startup. I'll take a look at editor that page when I get to it. Cheers.

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 would I find neighbours of a tile in a grid ? 1 Answer

Moving a character into a 2d int array 0 Answers

2d Array persists during multiple game sessions 2 Answers

I'm having trouble with grids and instantiating arrays of prefabs. 1 Answer

Tracking ojects grid positon 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