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 peacemaker · Oct 28, 2010 at 03:33 PM · gridboundsworld

Get position of the centre of each grid tile

I'm splitting a Plane into a grid using the following code:

public int numRows = 10;
public int numColumns = 10;
//get the level object
GameObject theLevel = GameObject.FindWithTag("Level");
//get the bounds
float levelWidth = theLevel.renderer.bounds.size.x;
float levelDepth = theLevel.renderer.bounds.size.z;
float realTileWidth = levelWidth / numRows;
float realTileHeight = levelDepth / numColumns;

Now I know the size of each tile, I can go through and add them all to my tile list. My problem now is trying to figure our the World position of the centre of each tile. What is the best way of doing this? I know I can get the centre of the Plane using:

theLevel.renderer.bounds.centre

But after that I'm at a loss of how to use that to figure out the centre of every grid tile?

Thanks in advance!

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

Answer by skovacs1 · Oct 28, 2010 at 05:41 PM

Assuming you know the exact size of your tiles, which assumes you know the size of your space space (note that bounds are axis-aligned and do not rotate with your object), you could do something like adding the column realTileWidth and row realTileHeight to the center of a known tile position on your space, adjusted by which space that is.

If you are using the center of a center tile(assumes an odd number of tiles), it would look something like:

//somewhere... Vector2 offset = new Vector2(numColumns / 2.0f - 1.0f, numRows / 2.0f - 1.0f);

//row and col are the row and column of the tile in question from 0-9 tilePosition.x = center.x + (col - offset.x) realTileWidth; tilePosition.z = center.z + (row - offset.y) realTileHeight

If you were using the bottom-left corner as a point of reference (make no assumptions about the number of tiles):

//somewhere... Vector2 offset = new Vector2(realTileWidth / 2.0f; realTileHeight / 2.0);

//row and col are the row and column of the tile in question from 0-9 tilePosition.x = col realTileWidth + offset.x; tilePosition.z = row realTileHeight + offset.y;

As for the height (tilePosition.y), that depends on your tiles.

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 peacemaker · Oct 28, 2010 at 05:47 PM 0
Share

Brilliant, that's exactly what I needed. Thanks for taking the time to answer this.

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

No one has followed this question yet.

Related Questions

Question about finding out edges of a box collider 0 Answers

How to Place and arrange 3D cards using Grid and Bounds 0 Answers

Are there any world bounds in Android? 0 Answers

Asset that can achieve a sonic game 4 Answers

Fastest way to draw a grid of textures 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