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
1
Question by kodagames · Dec 27, 2012 at 10:21 PM · transformgridcellneighbourconnected

Help with Game Mechanics

Im creating a game and need some help with the game mechanics. I created a Grid of gameObjects (transforms) and them gameObjects have a cell script attached which is supposed to tell what their neighbors are which Im failing at so far. The end result (which I just dont know how to figure out) is how to ultimately have the sphere I shoot in the picture below hit the other 1's and destroy all of the connected 1's (which only get destroyed if there are 3 or more).

This is my first time creating a grid and trying this type of game so its a learning experience (a tough one at that) and the code is also posted below.

alt text

CellScript:

 var adjacents : List.<Transform>; //not implemented yet???
 var  position : Vector3;
 var number : int;

GridScript:

 var cellPrefab : Transform;
 var size : Vector3;
 var grid : Transform[,];//cannot display 2d arrays in the inspector
 
 function Start () 
 {
     CreateGrid();
     SetRandomNumbers();
     SetAdjacents();
 }
 function CreateGrid()
 {
     grid = new Transform[size.x, size.z];
     
     for(var x = 0; x < size.x; x++)
     {
         for(var z = 0; z < size.z; z++)
         {
                 //We create a new Transform to manipulate later.
                 var newCell : Transform;
                 newCell = Instantiate(cellPrefab, new Vector3(x, 0, z), Quaternion.identity);
                 newCell.name = String.Format("({0},0,{1})",x,z);
                 newCell.parent = transform;
                 //this puts the position of itself into the var position of cellscript
                 newCell.GetComponent(CellScript).position = new Vector3(x, 0, z);
                 grid[x,z] = newCell;
             }
         }
     
 }
 function SetRandomNumbers()
 {
     for (var child : Transform in transform) 
     {
         var number = Random.Range(1,4);
         //assign the random number to the textMesh
         child.GetComponentInChildren(TextMesh).text = number.ToString();
         //assign the random number it is into the cell script
         child.GetComponent(CellScript).number = number;
     }
 }
 function SetAdjacents()
 {
     for(var x = 0; x < size.x; x++)
     {
         for(var z = 0; z < size.z; z++)
         {
             var cell : Transform;
             cell = grid[x,z];//the reason we are creating the grid
             var cS : CellScript = cell.GetComponent(CellScript);//abbreiate CellScript to cScr
             
             //access neighbors somehow and Add them to the list?
         }
     }
 }

I've come along way with this and I need help solving the rest (neighbors and destroying same type if 3 or more are connected). I know its a lot to ask but Im searched out.

Or is there a simpler way to group 1's, 2's and 3's so that I can connect them together and once hit with one destroy that group? I cant seem to find a solution.

shoot2.png (100.6 kB)
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
0

Answer by aldonaletto · Dec 28, 2012 at 03:39 AM

For each cell, you should check the 8 immediate neighbors, and add to the adjacents List those which had the same number:

 ...
 function SetAdjacents()
 {
     for(var x = 0; x < size.x; x++)
     {
        for(var z = 0; z < size.z; z++)
        {
          var cell : Transform;
          cell = grid[x,z];//the reason we are creating the grid
          var cS : CellScript = cell.GetComponent(CellScript);

          // set flags to show if there are neighbors at left, right, down and up:
          var hasLeft = x > 0;
          var hasRight = x < size.x-1;
          var hasDown = z > 0;
          var hasUp = z < size.z-1;
          // then verify the immediate neighbors:
          if (hasLeft) CheckNeighbor(cS, x-1, z);
          if (hasRight) CheckNeighbor(cS, x+1, z);
          if (hasDown) CheckNeighbor(cS, x, z-1);
          if (hasUp) CheckNeighbor(cS, x, z+1);
          // verify the diagonal neighbors, if you want to include them:
          if (hasLeft && hasDown) CheckNeighbor(cS, x-1, z-1);
          if (hasLeft && hasUp) CheckNeighbor(cS, x-1, z+1);
          if (hasRight && hasDown) CheckNeighbor(cS, x+1, z-1);
          if (hasRight && hasUp) CheckNeighbor(cS, x+1, z+1);
        }
     }
 }

 // this function verifies whether the neighbor at coordinates [x,z]
 // has the same number as the cell, and if so add it to the List:

 function CheckNeighbor(me: CellScript, x: int, z: int){
     // get the neighbor:
     var other: Transform = grid[x, z];
     // if the neighbor has the same number...
     if (me.number == other.GetComponent(CellScript).number){
        // add it to this cell's adjacents List:
        me.adjacent.Add(other);
     }
 }

NOTE: Simply declaring a List variable doesn't actually allocate it - you must do it explicitly with the new keyword (in the CellScript):

 var adjacents : List.<Transform> = new List.<Transform>();
 ...

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 kodagames · Jan 01, 2013 at 04:00 AM 0
Share

Thank You Very Very Very $$anonymous$$uch! Absolutely Amazing! Again Thank you for sharing it helps tremendously.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change each cell size based on amount of text in grid layout group 1 Answer

transform.position error 1 Answer

Finding adjacent tiles in a grid 1 Answer

Change transform.position.y in a loop 1 Answer

Drag to grid script help? 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