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 spaunn · Aug 20, 2013 at 12:58 PM · spawngridgrid based game

grid checking for objects

I have a series of planes set out on my terrain that form a grid on the floor, so far when you click a plane, it spawns a building chosen by the player using building IDs that are selected in the GUI, and all buildings have a box collider of the same size.

What I aim to do is setup some kind of modular building system for the player to mess around with, could anyone tell me how i could check what building is next to it in all 8 adjacent squares around to decide whether a building can be placed on the square thats clicked.

If a different building type is next to it or diagonal to it, it should not be allowed to be placed, otherwise the building will spawn as normal, any help?

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

Answer by ArkaneX · Aug 20, 2013 at 01:37 PM

I believe you can accomplish it using at least a few approach. Here's my suggestion.

You have to be able to detect the exact location of the plane that user just clicked. To be able to do this, you have to store planes in two-dimensional array:

 GameObject[,] planes = new GameObject[gridWidth, gridHeight];

Additionally, you need to add a component to the plane, let's name it PlaneInfo, with three properties: X (int), Z (int), BuildingTypeId (type of your building ID).

When initializing your grid, you have to do something like this:

 for(int x = 0; x < gridWidth; x++)
 {
     for(int z = 0; z < gridHeight; z++)
     {
         var plane = CreatePlane(); // your code to create plane
         var planeInfo = gameObject.GetComponent<PlaneInfo>();
         planeInfo.X = x;
         planeInfo.Z = z;
         planes[x,z] = plane;
     }
 }

When user clicks on a plane, you have to retrieve its PlaneInfo component, check for its grid coordinates, and check all the planes around it for building IDs:

 var planeInfo = clickedPlane.GetComponent<PlaneInfo>();
 var x = planeInfo.X;
 var z = planeInfo.Z;
 
 bool isBuildingPlaced = false;
 
 // checking planes around
 for(int xx = Math.Max(x,0); xx < Math.Min(x,gridWidth-1); xx++)
 {
     for(int zz = Math.Max(z,0); zz < Math.Min(z,gridHeight-1); zz++)
     {
         if(xx == x && zz == z)
         {
             continue; // we don't want to check triggering plane
         }
 
         var neighbor = planes[xx,zz];
         var neighborInfo = neighbor.GetComponent<PlaneInfo>();
         var neighborBuildingTypeId = neighborInfo.BuildingTypeId;
 
         // and now you can check if it is ok to place your building here
         // if it is ok, then don't forget to add building id to clicked plane
 
         PlaceBuilding();
         planeInfo.BuildingTypeId = buildingTypeIdSelectedInGui;
         isBuildingPlaced = true;
         break;
     }
     if(isBuildingPlaced)
     {
         break;
     }
 }

Please don't treat above code as a completed one, but rather as a base for your code. I'm not able to test it currently, so it might contains compile errors. But I believe it shows the general concept.

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

16 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

Related Questions

Create a grid on a cube? 0 Answers

Enemy object spawning before move action is complete :/ 1 Answer

How to find sprite at grid location? 1 Answer

Grid-based building system not working 1 Answer

grid-based block placement system with touch input 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