admin管理员组

文章数量:1294329

SOLVED - So, I was supposed to set the Z value of tileLocation Vector3Int to 0 before passing it through HasTile() because I was looping through each tilemap individually, which would all have their own z value of 0 (instead of comparing it to the tilemap anchor). After passing it through HasTile(), I then set the Z value to that of the tilemap.

I hope that makes sense to everyone. I'll be happy to clarify further if people are interested. I'm not sure if I should change my original code block or add a newly updated one. Let me know what the standard is if you'd like me to edit this post. Thanks guys!

-----------------------------------------------------------------------------------------------------------

I'm am fairly new and an aspiring game dev. I'm currently working on a 2d isometric tactical rpg with the Unity Game Engine and I've come across a peculiar problem.

I've created a Tilemap with the Tiled app. Every "height" difference is differentiated by the Z value (the imported Tilemap actually has 4 separate tilemaps with the base/water level being z = 0, ground level z = 1 etc) and I've imported into my game. I have several nested For-Loops to loop over each tilemap - each x value - each y value. This gets me every possible tile location within my Tilemap.

Using an if statement with the HasTile() method, I check to see if the possible tile location actually contains a tile. My problem is, every tile location comes back with a Z value of 0.

I've run a Debug.Log(tileLocation) directly before and after the IF statement with the HasTile() method. Before the IF statement, I get expected values of Z (between 0 and 3). Directly after the IF statement, those same Z values are all set to 0.

i.e if I had 3 tileLocations, they would change from:
Vector3Int(3, 5, 1) -- Vector3Int(3, 5, 0)
Vector3Int(13, 2, 2) -- Vector3Int(13, 2, 0)
Vector3Int(4, 18, 3) -- Vector3Int(4, 18, 0)

Things that I've tried:

1 - removing the var of tileLocation (I thought maybe it was resetting somehow because it was a local variable)

2 - ran an empty if statement to see if it was actually the IF statement causing the problem and not the HasTile() method

3 - removed the !map.ContainsKey method (still had the problem)

4 - I've tried tweaked and adjusting a bunch of other things but didn't have any success either

My goal is to locate every tile and Instantiate an invisible "overlayTile" overtop so that I can interact with them. i.e move a character or target a tile for an AoE spell.

Am I missing anything? Is that a weird quirk of HasTile() that I need to work around?

Any ideas/help would be greatly appreciated. Thanks!

Here is the relevant code:

    void Start()
    {
        tileMaps = AssembleTilemapLayers();
        tileMaps.Reverse();
        map = new Dictionary<Vector3Int, OverlayTile>();

        for (int i = 0; i < tileMaps.Count; i++)
        {
            var tileMap = tileMaps[i];
            var bounds = tileMap.cellBounds;
            var z = (int)tileMap.transform.position.z;


            // looping through all map tiles
            for (int y = bounds.min.y; y < bounds.max.y; y++)
            {
                for (int x = bounds.min.x; x < bounds.max.x; x++)
                {
                    tileLocation = new Vector3Int(x, y, z);


                    if (tileMap.HasTile(tileLocation) && !map.ContainsKey(tileLocation))
                    {
                        var overlayTile = Instantiate(overlayTilePrefab, overlayContainer.transform);
                        var cellWorldPosition = tileMap.GetCellCenterWorld(tileLocation);

                        overlayTile.transform.position = new Vector3(cellWorldPosition.x, cellWorldPosition.y, cellWorldPosition.z + 1);
                        overlayTile.GetComponent<SpriteRenderer>().sortingOrder = tileMap.GetComponent<TilemapRenderer>().sortingOrder;
                        overlayTile.gridLocation = tileLocation;

                        map.Add(tileLocation, overlayTile);
                    }
                }
            }
        }
    }

本文标签: cSOLVEDTilemapHasTile() method Vector3Int changes Z value to 0Stack Overflow