Seanba / SuperTiled2Unity

Imports Tiled files to Unity. Better than regular Tiled2Unity.
MIT License
655 stars 84 forks source link

The grid is not aligned after being imported into unity #269

Open 173198205 opened 4 months ago

173198205 commented 4 months ago

One feature I want to implement is that when you click on a tile, you can know which tile you clicked on and show a selected status in the center of that tile. But I found that the grid is not aligned with unity's tilemap grid, causing a misalignment. What should I do? My test code:

if (Input.GetMouseButtonDown(0)) { Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); var cellPos = Tilemap.WorldToCell(pos); var tile = Tilemap.GetTile(cellPos); if (tile != null) { var wPos = Tilemap.CellToWorld(cellPos); select.gameObject.transform.position = wPos; Debug.Log($"click tile:{tile.name}-{cellPos}"); } Debug.Log("GetMouseButtonDown"); }

Snipaste_2024-05-13_18-12-23 Snipaste_2024-05-13_18-11-27 Snipaste_2024-05-13_18-12-51 Snipaste_2024-05-13_18-13-25

173198205 commented 4 months ago

Then I tested the click myself with a ray

Seanba commented 4 months ago

Hi there, @173198205. Sorry I'm getting back to you so late.

I think this is an error with Super Tiled2Unity where I'm not taking into account the different coordinate systems between Tiled and Unity. Normally you wouldn't notice but if you're trying to click on a tile I can see that being a problem.

Can you test something out for me? Try changing your Tilemap component so that you have a Custom orientation and offset the tilemap by half of your tile width (64 in your case, I think). That's not a real fix but it should help me track down what I need to do.

image

Would it possible for me to look at your project? I'd like to make sure the fix I come up actually works for you. Thanks.

173198205 commented 4 months ago

I am a novice and do not understand many things, the project is just a simple test that can show you. It doesn't seem to work if you change the offset as you said. I later thought of a way to implement it, that is, to abandon the idea of tilemap, convert the map into an object, and use rays and colliders to detect clicks, which I basically realized. project2021.zip