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 SubatomicHero · Feb 26, 2013 at 03:37 PM · listarraysgrid

Grid obstacle detection

Hi all,

I am having a problem with obstacle detection on my grid. My grid size differs depending on the current level of the game. Currently my grid is instantiated piece by piece and placed inside a List. Attached to the grid pieces is this class:

 public class gridtile : MonoBehaviour
 {
     // This class will be used so we can modify the isOccupied flag
     // if the grid section is occupied, the cube cant move onto it
     public bool isOccupied = false;
     public bool haslightsOn = false;
     
     public gridtile north;
     public gridtile south;
     public gridtile east;
     public gridtile west;
 }

Once the obstacles have spawned on a part of the grid the isOccupied flag changes from false to true. I am trying to figure out how to initialize the north, south etc pieces dependant on the current tile my player is on. I think this sort of looks like a linked list. My player object moves from grid piece to grid piece. The player class also has a data member called gridtile currentTile. I am not sure also how to "read ahead" in the list so that for example this code may work:

 if (currentTile.north.isOccupied)
 {
     canMoveNorth = false;
 }
Comment
Add comment · Show 2
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 robertbu · Feb 26, 2013 at 04:57 PM 0
Share

Is this a rectangular grid? What order you instantiate the pieces?

avatar image Tarlius · Feb 26, 2013 at 05:09 PM 0
Share

I want to jump on the array-train too, but if you're doing huge irregular shapes this could be an interesting problem :)

I'm thinking something like Hashtables, but it seems horribly inefficient... Another approach could be attaching coordinates to the class then searching for them, but again, slooow.

But tbh all signs point to a 2D array. Is there a reason you're using a list over a 2D array?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tarlius · Feb 26, 2013 at 05:20 PM

This should get you going. Note that you'll have to add null checks when checking if you can move, since the ones around the outside will not have adjacent tiles (you could wrap them though if you want?)

 Block[] blocks = new Blocks[4,4];
 
 for(int i = 0; i < 4; i++) {
     for(int j = 0; j < 4; j++) {
         blocks[i,j] = new Block();
     }
 }
 
 for(int i = 0; i < 4; i++) {
     for(int j = 0; j < 4; j++) {
         if(i + 1 < 4) blocks[i,j].North = blocks[i +1,j];
         if(i - 1 >= 0) blocks[i,j].South = blocks[i - 1,j];
         if(j + 1 < 4) blocks[i,j].East = blocks[i,j + 1];
         if(j - 1 >= 0) blocks[i,j].West = blocks[i,j - 1];
     }
 }

I'm a little disappointed we aren't making massive irregularly shaped continents with portals though :(

Comment
Add comment · Show 9 · 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 SubatomicHero · Feb 26, 2013 at 05:21 PM 0
Share

lol thanks for the code, I'll give it a whirl. Also a portal idea sounds amazing!! I may have to "integrate" that into my game :P

avatar image Tarlius · Feb 26, 2013 at 05:23 PM 0
Share

zomg theiff! :D

avatar image SubatomicHero · Feb 26, 2013 at 05:24 PM 0
Share

I assume this code is:

1). creation of the grid 2). Checking the next possible movement (north, south etc) is possible?

avatar image Tarlius · Feb 26, 2013 at 05:27 PM 0
Share

No, create the grid that assign all the references inside the class for North, South, East, West. So you can do: if (currentTile.north.isOccupied) { can$$anonymous$$oveNorth = false; }

Although as I mentioned, you'll need to check whether .north is null before you try .isOccupied

avatar image SubatomicHero · Feb 27, 2013 at 10:26 AM 0
Share

A couple of problems:

1). $$anonymous$$y gridtile class is derived from monobehaviour, so I cant create new from that. 2). I've supplied my code for your perusal, and maybe you can see where I can make the changes?

 public class gridtile : $$anonymous$$onoBehaviour
 {
     // This class will be used so we can modify the isOccupied flag
     // if the grid section is occupied, the cube cant move onto it
     public bool isOccupied;
     public bool haslightsOn;
     
     public gridtile north;
     public gridtile south;
     public gridtile east;
     public gridtile west;
     
     void Start()
     {
         isOccupied = false;
         haslightsOn = false;
     }
 }

And I have a separate class the builds the grid like so:

public class gridBuilder : $$anonymous$$onoBehaviour { public static List gridList; public static ArrayList lgtPos; public static short lightsOnCount; public Transform grid;

 public static bool gridIsBuilt;
 public static bool lightsAreOn;
 
 
 // Use this for initialization
 void Start() 
 {
     gridList = new List();
     lgtPos = new ArrayList();
     gridIsBuilt = false;
     lightsAreOn = false;
     
     StartCoroutine(createSquareGridTimed());
 }
 IEnumerator createSquareGridTimed()
 {
     short all = 0;
     // The values used in the loops will change depending on the level, so the grid is different
     for (short i = 0; i < squareGridSize(level$$anonymous$$anager.level); i++)
     {
         for (short j = 0; j < squareGridSize(level$$anonymous$$anager.level); j++)
         {
             // Add each piece of the grid to the screen and animate it
             GameObject g = Instantiate(grid.gameObject, new Vector3(i, 15, j), Quaternion.identity) as GameObject;
             iTween.$$anonymous$$oveTo(g, new Vector3(i, 0, j), 0.8f);
             
             // Add the gameobjects to the list, to be used by all other classes
             gridList.Add(g);
             all++;
             yield return new WaitForSeconds(0.15f);
             
         }
     }
     // Set the various variables needed for the other classes
     gridList.TrimExcess();
     yield return new WaitForSeconds(0.5f);
     lightsOnCount = (short)gridList.Count;
     
     gridIsBuilt = true;
     StartCoroutine(toggleLightingTimed());
 }
 
 IEnumerator toggleLightingTimed()
 {
     // gridLighter must send the lights position to the data$$anonymous$$anager class
     // it needs to be retrieved by the cubeCollider class
     Debug.Log("toggleLighting() called");
     Transform go;
     for (short i=0; i < lightsOnCount; i++)
     {
         go = gridList[i].transform.FindChild("GridLight");
         gridtile tile = gridList[i].gameObject.GetComponent<gridtile>();

         if (null == go)
         {
             // Handle the error if the piece of the grid cant be found
             Debug.Log("Light not found!");
             yield break;
         }
         
         go.light.enabled = true;
         go.light.audio.Play();
         tile.haslightsOn = true;
         lgtPos.Add(gridList[i].transform.position);
         yield return new WaitForSeconds(0.05f);
     }
     lightsAreOn = true;
 }
Show more comments

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

10 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

Related Questions

A node in a childnode? 1 Answer

Grid Based Map Object-Array vs Arrays only 1 Answer

using char to extract integer numbers from a list - [UnityScript] 1 Answer

Is there a way to remove array entries in the editor? 4 Answers

An Array within array 2 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