Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 genowhirl9999 · Apr 17, 2019 at 08:05 AM · raycastingtilemapmousepositiontileisometric

Selecting a tile with z as y isometric tilemaps?

So I would like to select a tile with the mouse using the Isometric Z as Y tilemap. The issue I'm having is that using the WorldToCell function does not return the cell position of the tile being clicked on because it isn't accounting for the additional y value from the z-axis. So clicking on a tile with a large z offset often gives a cell behind it rather than the cell the tile you clicked on actually belongs to.

I attempted raycasting but was unable to get the specific tile being collided with from the tilemap collider.

This only seems to be an issue with the Z as Y tilemap because tiles on the other maps always line up with their respective cells. There doesn't seem to be any way to account for offset tiles in general actually, I've noticed numerous other (unanswered) questions with offset and oversized tiles not lining up with their respective cell positions.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mishasniper · Feb 17, 2020 at 05:39 PM

For who Mouse Position to Isometric Z as Y tilemap is still actual :

Use Grid.WorldToCell instead of Tilemap.WorldToCell !

And dont forget set mouse world position to zero (pos.z = 0) ( or other positive value for height offset). Overwise tile be invisible.

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         pos.z = 0;
         Vector3Int currentCell = _Grid.WorldToCell(pos);
         var tile = _Tilemap.GetTile(currentCell);
         _Tilemap.SetTile(currentCell, _TileToSet);
     }
 }

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
avatar image
0

Answer by mcwind168 · Sep 03, 2019 at 02:34 AM

If you use a standard isometric z as y tilemap configration, that is, cell size of grid is (1,0.5,1) and transparency sort mode in project settings/graphics is 'Custom Axis' (0,1,-0.26). You can 'pick' a tile like this:

 void Pick() {
             if (Input.GetMouseButtonDown(0)) {
                 var wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     
                 for (int z = 0; z < 10; z++) {
                     wp.z = z;
                     var cell = _grid.WorldToCell(wp);
                     var tile = _tileMap.GetTile(cell);
                     if (tile != null) {
                         Debug.Log(cell + " " +  tile.name);
                     }
                 }
             }
         }

The API Grid.WorldToCell() consider 'z' component of the world point when calculate cell position. So when z is 0, it pick tiles at first floor, 1 for second floor and so on. In my code above, I try to pick 10 floors and log all tile if any.

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 genowhirl9999 · Sep 03, 2019 at 06:14 AM 0
Share

As I tried to explain this doesn't work. If you have a tile 10 units high and click on it the "column" of tiles you'd be getting here will not include the one you've clicked on. It will be the column of tiles of the point on the grid on which you've clicked, usually somewhere behind where you've clicked.

avatar image
0

Answer by Draelent · Oct 25, 2020 at 09:25 PM

Ok, I used both answers here but it was not enough to solve the problem. Considering the same "isometric z as y" tilemap config, cell size (1,0.5,1) and transparency sort (0,1,-0.26). Consider that your z is [0, 10[

 // Get mouseClick using new input system (you can also use the old version of previous answers)
 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
 mousePos.z = 0; // Just for debug clarity, but not mandatory
 Vector3Int gridCell = grid.WorldToCell(mousePos);
 // Using tilemap instead works aswell
 // Vector3Int tileCell = tilemap.WorldToCell(mousePos);
 
 Debug.Log("mousePos : " + mousePos + " cellPos_0  : " + gridCell);
 
 // You can use that loop to get all tiles of current pile of tiles,
 // starting from the grid cell (with z = 0) that you clicked.
 for (int z = 10; z > -1; z--){
     gridCell.z = z;
     var tile = tilemap.GetTile(gridCell);
     if (tile != null) {
         Debug.Log("grid" + gridCell + " found " +  tile.name);
         // break; // If you need only the top most tile of that pile
     }
 }
 
 // You can use this loop to check all the tiles that could be 
 // hiding the grid cell with (z = 0) that you clicked.
 // We start from the front most tile which with an elevation of 9
 // and would correspond to a 2.25 below current y
 // and then we decrease z as we increase y by 0.25f (half cell height)
 mousePos.y -= 2.5f; // to be adapted depending on your max Z and the start of the loop below
 for (int z = 10; z > -1; z--){
     gridCell = grid.WorldToCell(mousePos);
     gridCell.z = z;
     var tile = tilemap.GetTile(gridCell);
     if (tile != null) {
         Debug.Log("grid Z" + gridCell + " found " +  tile.name);
         // break; // If you need only the first tile encountered
     }
     mousePos.y += 0.25f;
 }


The idea is that since your cell height is 0.5 , and due to the layout, you should check with z+1 and y-0.25 each time, to see if you find any tile that would be hiding current grid cell. I used the reverse order to be able to stop as soon as we find the front cell.

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

108 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 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 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 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 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 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 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

tile palette offset tiles stacking not lining up 0 Answers

How to get adjcent tiles (horizontal or vertical) in an isometric tilemap ? 1 Answer

Rotate Object with mouse in RTS style game 2 Answers

Isometric grid snapping 0 Answers

Get position from Isometric TileMap 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