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
0
Question by Ekta-Mehta-D · Mar 22, 2013 at 10:23 AM · 2dgridneighbour

Determine neighbours of grid cell :2D

I have generated grid with specific pattern(controlled grid)..And now i want to make list of all neighbours of perticular cell.

I have created some of the cell with transform and some of them filled with sphere prefab.

When i shoot sphere , i need to check the sphere with same color. And if they are more than 3 then i want to destroy them. But i do not understand the way how to manage neighbours for each cell.

How will i get neighbours of the collided object in shooting script associated with each sphere ?? Pleaze somebody guide me.

Code to generate Grid:

 public var sphere : Transform[];   
    private var B : int = 0; 
    private var G : int = 1;  
    private var P : int = 2; 
    private var R : int = 3; 
    private var Y : int = 4;  
    private var E : int = 5;   
    
    public var  m : int= 12;
    public var n : int = 12;
  
    public var radius : float = 0.5f;
    public var useAsInnerCircleRadius : boolean = true;
  
    private  var offsetX : float;
    private var offsetY : float;
  
    private var formation  = [   [E ,E ,E ,E, E, E, B, Y, Y  ],
                                    [E ,E ,E ,E, E, E, Y, Y, Y  ],
                                  [E ,E ,E ,E, E, E, Y, G, G  ],
                                    [E ,E ,E ,E, E, E, P, R, G  ],
                                 [E ,E ,E ,E, E, E, R, R, R  ],
                                 [E ,E ,E ,E, E, E, R, R, R  ],
                                  [E ,E ,E ,E, E, E, P, B, B  ],
                                  [E ,E ,E ,E, E, E, B, Y, B  ],                                 
                                  [E ,E ,E ,E, E, E, G, E, Y  ] ];
                                          
    function Start() {
       
       var go : GameObject = GameObject.Find("Grid");
       var unitLengthv : float = ( useAsInnerCircleRadius )? (radius / (Mathf.Sqrt(3)/2)) : radius;
  
       offsetX =  Mathf.Sqrt(3);
       offsetY =  1.5f;
  
       for(var i : int = 0; i < formation.length; i++ ) {
          for(var  j : int = 0; j < formation[i].length; j++ ) {
            var  hexpos : Vector2 = HexOffset( i, j );
             var pos : Vector3 = new Vector3( hexpos.x, hexpos.y, 0 ) + transform.position;
             var tempsphere : Transform = Instantiate(sphere[formation[i][j]], pos, Quaternion.identity );
             tempsphere.transform.parent = go.gameObject.transform;
             if(formation[i][j] != E)
             {
                     tempsphere.transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
             }
          }
       }
    }
  
     function HexOffset( x : int, y : int) : Vector2 
     {
           var position : Vector2 = Vector2.zero;
      
           if( y % 2 == 0 ) {
              position.x = x ;//* offsetX;
              position.y = y ;//* offsetY;
           }
           else {
              position.x = ( x + 0.5f );// * offsetX;
              position.y = y;// * offsetY;
           }     
           return position;
    }

Here E stands for empty space. If the bullet sphere is not collided with same color, then it wil be placed at nearest empty position..

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Graham-Dunnett · Mar 22, 2013 at 11:44 AM

formation[i][j] has neighbours:

 formation[i-1][j  ]
 formation[i  ][j-1]
 formation[i+1][j  ]
 formation[i+1][j+1]

However you need to check that i-1 is not negative, that j-1 is not negative, and that i+1 and j+1 are not larger than the array.

Comment
Add comment · Show 1 · 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 Ekta-Mehta-D · Mar 22, 2013 at 11:49 AM 0
Share

Thanks sir.. But in my shooting script, How can i deter$$anonymous$$e the neighbours of the collided object ? I am very confused. $$anonymous$$y shooting script is associated with sphere prefab.

avatar image
0

Answer by robertbu · Mar 22, 2013 at 04:46 PM

When you posted your question, you did not include a reference to a picture of the layout. The code you are using above is represented as a rectangular grid (2D array), but you are actually using a hexagonal grid. @Graham Dunnet's solution will work just fine for a rectangular grid, but it won't work for your code.

There are several different ways you can approach the problem. One way would be to compare the position of the object you collided with to the other game objects. When I say 'position', I mean the actual position of the game object, not the contact point. In a hexagonal grid, all the neighbors are equal distance from each other, so just pick a distance slightly larger than the distance you used in the layout of your grid.

Comment
Add comment · Show 1 · 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 Ekta-Mehta-D · Mar 23, 2013 at 04:21 AM 0
Share

ohk sir.. :(

avatar image
0

Answer by Maulik2208 · Mar 25, 2013 at 07:25 AM

Assuming that you are developing something like Bejeweled......And if it is the case then Using a 2d array is a Good idea and for perfectly checking for the neighbors then you can opt from several ways.....you can use Tagging as well as check the texture of the neighbor as well i was confused at the same point and got some help from here...... Bejeweled Neighbour

<-------- this link will lead you to my question and also there is a solution posted by @CodeMasterMike hope that solution will help you also.........Have a Happy coding.....

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

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

13 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

Related Questions

A node in a childnode? 1 Answer

Glitching between box colliders. One is kinematic... Help! 1 Answer

A Question about sprites 3 Answers

How to make 2d distortion? 2 Answers

Get position from Isometric TileMap 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