Unity-Technologies / 2d-extras

Fun 2D Stuff that we'd like to share!
Other
1.54k stars 342 forks source link

Instantiating Rule Override Tile in Script Not Working #289

Open HahnJustin opened 3 years ago

HahnJustin commented 3 years ago

Hi, I am writing some code that procedurally generates an exit to a map. I have it set up to have the exit door be a rule overrule tile that overrides an example sibling tile (as defined in the 2d-techdemos repo). The sibling tile generates great through script and placed manually the overrule tile works. But for some reason using TileMap.SetTile() to place the rule overrule tile makes it so that the overrule tile does not interact at all with the sibling tile. Here are some pictures then the small snippet of code that instantiates the rule overrule tile. Also my unity version is 2020.1.10f1 and all help is appreciated, Thanks!

Google Drive to Screenshots

    public Tilemap topMap;
    public Tilemap bottomMap;
    public RuleTile topTile; //The sibling tile
    public RuleTile bottomTile;
    public Tile bottomTile2;
    public RuleOverrideTile transitionTile; //The exit tile
if (PathUtilities.IsPathPossible(leftTop, rightBottom))
        {
            NNConstraint wallConstraint = new NNConstraint();
            wallConstraint.walkable = false;
            Vector3 doorPosition = AstarPath.active.GetNearest((Vector3)leftTop.position, wallConstraint).position;
            Vector3Int doorIntVector = new Vector3Int((int)doorPosition.x, (int)doorPosition.y, (int)doorPosition.z);
            topMap.SetTile(doorIntVector, transitionTile);
            Instantiate(sceneTransitionPrefab, doorIntVector, Quaternion.identity);
            Debug.Log("GENERATIONMANAGER: DoorVector:" + doorIntVector);
            PlayerController.Instance.gameObject.transform.position = (Vector3)rightBottom.position;
        }
        topMap.RefreshAllTiles();
ChuanXin-Unity commented 3 years ago

Sorry, but I do not quite see what the issue is from all the screenshots. It seems like there are at least two Tilemaps, with one overlapping the other? If possible, could you make it clearer how the override tile should interact with the sibling tile?

ChuanXin-Unity commented 3 years ago

I believe that you can link or insert the images in the post too, which will be helpful!

HahnJustin commented 3 years ago

Overrule tile error5

Overrule tile erase

I updated the code to look like this:

        if (PathUtilities.IsPathPossible(leftTop, rightBottom))
        {
            //Instantiate(sceneTransitionPrefab, (Vector3)rightBottom.position, Quaternion.identity);
            NNConstraint wallConstraint = new NNConstraint();
            wallConstraint.walkable = false;
            Vector3 doorPosition = AstarPath.active.GetNearest((Vector3)leftTop.position, wallConstraint).position;
            Vector3Int doorIntVector = new Vector3Int((int)doorPosition.x, (int)doorPosition.y, (int)doorPosition.z);
            topMap.SetTile(doorIntVector, transitionTile);
            for (int i= -1; i <=1; i++ )
            {
                for(int j = 0; j <= 1; j++)
                {
                    if (0 != j || 0 != i)
                    {
                        topMap.SetTile(new Vector3Int(doorIntVector.x + i, doorIntVector.y + j, 4), topTile);
                        Debug.Log("GENERATIONMANAGER: i&j:" + i + " " + j);
                    }
                    topMap.RefreshTile(new Vector3Int(doorIntVector.x + i, doorIntVector.y + j, 4));
                }
            }

            Instantiate(sceneTransitionPrefab, doorIntVector, Quaternion.identity);
            Debug.Log("GENERATIONMANAGER: DoorVector:" + doorIntVector);
            PlayerController.Instance.gameObject.transform.position = (Vector3)rightBottom.position;
            topMap.RefreshTile(doorIntVector);
        }
        yield return new WaitForSeconds(0.05f);
        topMap.RefreshAllTiles();

But essentially the issue is that in the editor I cannot even erase the override or sibling tiles I made using SetTile even though they exist on the same tilemap, the topMap as erasable tiles. The ground in this picture is on the bottomMap while the walls are on the topMap. Despite that the script-created tiles do not interact with the other tiles on the same topMap at all. The interaction that I was expecting was for the walls to interact like I defined in the rule override tile and the example sibling tile. I am realizing this might not specifically an issue with override tiles as I am now creating sibling tiles in this new code that do not interact with other tiles in such a way. In addition all other tiles in this scene are made in script as well but they behaved as expected. Also this is in a co-routine as shown by the yield return new WaitForSeconds(0.05f); would that change anything? Anyway this really did not seem like intended behaviour so I made this ticket. Thanks for checking it out.

Here's another gif to help visualize the issue.

Overrule tile erase2

ChuanXin-Unity commented 3 years ago

Thanks for the update!

I have some questions about the code:

Vector3Int doorIntVector = new Vector3Int((int)doorPosition.x, (int)doorPosition.y, (int)doorPosition.z); and topMap.SetTile(new Vector3Int(doorIntVector.x + i, doorIntVector.y + j, 4), topTile);

It seems that you are setting the Tiles at a specific Z value. I assume that this is intended as you want to layer Tiles over other Tiles in the same Tilemap? They would need to be erased using the same Z values as well.

image

At the bottom of the Tile Palette, you can access the Brush Inspector and the Z position of the Brush. Is the Z position set to the same Z values as you have set for your Tiles?

Also, the Rule and Rule Override Tiles generally do not interact with other Tiles with different Z positions, even if they are in neighbouring X and Y positions, so that can affect what you are seeing.

HahnJustin commented 3 years ago

That worked! Thank you so much I did not realize that the eraser tool worked only for specific z values. I just switched the 4 to 0 (The z-value of the other tiles). It seems this interaction is intentional, but I think it could be more clearly documented as as soon as I realized I could not erase those tiles I just assumed it was a bug.

Thanks again!