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 iilleen_unused · Jun 09, 2013 at 08:59 PM · gameobjecthexworld coordinates

Game Object - world space to grid coordinates

I've asked this question once but made it unclear so I want to rephrase it.

I have a way of converting world position to hex coordinates. Hex map is stored as a 2D array and it's represented on the scene via a projected green hex grid. I want to know what hex fields are 'occupied' by game objects on the scene.

alt text

Picture related - I'd like to know over which hexes the white box is.

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

Answer by robertbu · Jun 09, 2013 at 09:44 PM

This is a harder problem, and I cannot think of a perfect solution. What you could do is:

  1. Do a raycast or just a calculation to find the hex that contains the center of the overlaying game object.

  2. Recursively walk outward from the found hex and raycast upward from the center of the each adjacent hex. Note since you are checking a specific object at this point, you can do a a cheaper Collider.Raycast(), rather than a Physics.Raycast(). The walk outward is similar to a flood fill algorithm.

  3. If the Raycast() fails, you will need to do a check for partial coverage. Doing a Collider.Raycast() from the six corners of the hex will get most objects. Doubling it to 12 raycasts should get almost all objects. You could use a Physics.SphereCast() if you wanted extra insurance against very narrow objects, but no combination will be 100 percent sure, and performance may be an issue.

Comment
Add comment · Show 2 · 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 iilleen_unused · Jun 09, 2013 at 10:06 PM 0
Share

I only need this as a dev tool, so that after creating/updating the scene I will be able to one-time run this method and it will save the results to a text file or something, so performance is not an issue for me.

I also thought about making an empty object with a sphere collider and jumping with it through all the hexes on the map. Sadly I don't know how to 'manually' check the collisions and the for loop is too fast for OnTriggerEnter/OnTriggerStay to catch any collisions.

avatar image robertbu · Jun 09, 2013 at 10:41 PM 0
Share

You can use Physics.SphereCast() to cast your sphere. No need to use a real sphere collider. Note the SphereCast() will miss the corners. If this is an edit time tool, you could just do a SphereCast() and/or Raycast() for all the hex positions and not worry about flood-fill-like behavior.

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

14 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

Related Questions

Get world Co-ordinates from an image in a gameobject 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Managing thousands of entities (for an RTS-like game)? 1 Answer

Position of game objects on a hexagonal grid 2 Answers

Decrease variable after pressing a button 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