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 question was closed Apr 11, 2021 at 08:08 PM by studenyfilip for the following reason:

The question is answered, right answer was accepted and solution can be located here https://github.com/FilipStudeny/Cellular-Automata---Island-generation

avatar image
0
Question by studenyfilip · Apr 07, 2021 at 09:09 PM · tilemapgridtileneighbouradjacent

How would I find neighbours of a tile in a grid ?

Hello everyone, I am currently trying to create a small builder game.

So I have arrived at a massive problem.

SOLUTION CAN BE FOUND HERE !!! https://github.com/FilipStudeny/Cellular-Automata---Island-generation


I can generate a grid that is made out of cubes each cube has its own script (Tile.cs) and that script holds references to its four neighbours (Above,Right,Left,Bellow), however I have a problem figuring out how to add these neighbours to the tiles.


I don't want to use Raycast or Sphere for neighbour detection as it slows down performance.


This is my code for the grid generation.

 public class WorldGenerator : MonoBehaviour
 {
     public int mapSize;
 
     public GameObject tile;
     public List<GameObject> tiles;
 
     // Start is called before the first frame update
     void Start()
     {
         GenerateStartingGrid();
         AssignTileNeighbors();
     }
 
     void GenerateStartingGrid() 
     {
         for (int x = 0; x < mapSize; x++)
         {
             for (int y = 0; y < mapSize; y++)
             {
                 if ( mapFilled[x,y] == 0) 
                 {
                     Vector3 tilePosition = new Vector3(-mapSize / 2 + x, 0, -mapSize / 2 + y);
 
                     GameObject newTile = Instantiate(tile, tilePosition, Quaternion.Euler(Vector3.right * 90f)) as GameObject;   
                     newTile.transform.SetParent(transform, false);
                     
                     tiles.Add(newTile);        
                 }
                 else { continue; }
                 
             }
         }
     }
 
     void AssignTileNeighbors() 
     {
         for (int x = 0; x < mapSize; x++)
         {
             for (int y = 0; y < mapSize; y++)
             {
                //Left neighbr
                 if (x - 1 >= 0)
                 {
                    
                 }
                 //Right neighbor
                 if (x + 1 < mapSize)
                 {
                    
                 }
                 //Below neighbor
                 if (y - 1 >= 0)
                 {
                     
                 }
                 //Above neighbor
                 if (y + 1 < mapSize)
                 {
                    
                 }
             }
         }
     }
 }

and this is my code for the tile.

 public class Tile : MonoBehaviour
 {
     public int ground;
 
     public GameObject neighbor_UP;
     public GameObject neighbor_RIGHT;
     public GameObject neighbor_LEFT;
     public GameObject neighbor_DOWN;
 }
 


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

  • Sort: 
avatar image
1
Best Answer

Answer by VARUN88 · Apr 11, 2021 at 07:30 PM

You can set the cubes in order in start and use their rows and columns to get nearby points like the gameobject[row] [column-1] etc.

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

Follow this Question

Answers Answers and Comments

120 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stack components on a Tile/Tilemap 0 Answers

TileMap.GetTile not working as expected 0 Answers

How can I access to a tile properties in a Tilemap? 0 Answers

Finding adjacent tiles in a grid 1 Answer

Correct use of setTile to add tile to Tilemap 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