2D Roguelike Tutorial Help with using Tilemap

Hello, I am new to coding, and need some direction. I have been following the “2D Roguelike Tutorial” and it’s great, but I wanted to change 1 thing about the code.

The code generates an 8x8 grid with outerwalls, innerwalls, and items and enemies, mostly randomly generated.
I would rather build my own levels, and I want to use Tilemaps. The main issue I am running into is the player will not pick up the tilemap as a collider.
I have researched a bit and browsed the forums, but could not find anything to help me.
I tried changing the collider type of the tilemap to a 2D box collider specified in the code for moving object, but even that won’t stop movement either. I think I have to use “using.unityengine.tilemaps” , but I can not figure out how to get the tilemap to be read by the script to detect the Colliders.

Main Questions:

Can I use this script for tilemap collision? If so where should I start?
The Grid based movement is wonderful and I want to keep it the same, would I add LayoutGrid instead of InitialiseList? If so, how do I add LayoutGrid to C# code?
How do I enable my Script to “pick up” a tilemap / Level and “Read” where the colliders are for movement?
(I have been trying to use public Tilemap tilemap; )
Am I completely wrong about needing to get the script to read the tilemap? Am I missing something obvious?

Conclusion

Honestly, I just want to be able to build a level with tilemaps and then move around in that level, with grid based (pokemon style) movement. I will worry about the more complicated stuff later, I have just been stuck for 3 days trying to figure this out haha.

I followed along with the tutorial, but after I completed the script I Copied the Tutorial’s code for the comments to help me understand the code.
I attached the code in files at the bottom but I’m not sure how to implement the code in an organized fashion on the forums like I’ve seen

Thank you so much taking time out to help a hopeless newbie.
-casperic

6173095–675667–MovingObject.cs (5.56 KB)
6173095–675673–Player.cs (7.09 KB)
6173095–675676–BoardManager.cs (7.69 KB)

There is a specific collider type for tilemaps called a tilemap collider which is what you should be using. What it does is it uses the default collider for the sprite you’re using as the tile and merges them all together.

Now if you’re having trouble getting your player to recognize the collider and it does recognize other colliders, it is probably a layering issue. You probably have your player set to a layer that doesn’t recognize the tilemap layer as a collision. You have to go into your project settings to fix that up, or change the layers of one or the other, but probably better to check the project settings. Now, if the player isn’t recognizing any colliders, then you probably have the player collider setup wrong.

Edit:
Looking at your boardmanager, it looks like it is designed to use discrete gameobjects rather than a tilemap. Since you’re generating your tilemaps, that’s not that big a deal as you still ultimately will have some easy reference to what goes where, but instead of gameobjects it will be probably a simpler structure. Hard to say with just the scripts how this all works together though.

1 Like

I know its not set up for tilemaps but honestly your answer confused me more haha.
how do I go about setting it up to use tilemaps?
how do i know what layer my tilemap is on? am i looking for a sorting layer or the z axis? or so i have to specify it in code?
I looked in the Project settings and by default all of the layer collisions are checked? Is that what youre talking about?

Setting it up for a tilemap you’d want a routine that converts a raw set of data representing what the tiles should be to actual tiles. Let’s say for instance you want super simple and have a 2 dimensional array of ints representing what your tiles should be. This routine would then be given a set of tiles for each of these potential int values, and you just run through the array setting the tilemap using the 2 dimension array as a guide using the SetTile function of the tilemap. As a quick start, just have script that has a public tile or two that you can set to tiles, and have that routine in start just set a 10x10 area of your tilemap say with that tile or tiles.

public Tile tile1;
public Tile tile2;
public Tilemap tilemap;

private void Start()
{
    for (int x = 0; x < 10; x++)
    {
        for (int y= 0; y < 10; y++)
        {
            if (x >= 5)
                tilemap.SetTile(new Vector3Int(x, y, 0), tile1);
            else
                tilemap.SetTile(new Vector3Int(x, y, 0), tile2);
        }
    }
}

So put that on an object in your scene, set tile1 and tile2 and tilemap appropriately, then on start you should get 2 side by side 5x10 regions corresponding to your 2 tiles. Change that logic to drive from an array or whatever and you can start building levels from tiles.

As to layer, in the inspect, near the top there is the name of the object, the tag, and then the layer. Sorting layers and layers are different things here. Sorting layer determines the display of what is on top and what is below, while layer determines things like lighting and collision interactions. If you aren’t aware of this though, you may have setup your collider incorrect for this. Looking at the code, I suspect you have it set to trigger mode, which means collisions are just triggers for events, they don’t stop movement automatically. If you want to have them stop movement automatically they should be not set to trigger, but it sounds more like you want to script in a move back for your application, but can’t be sure.

1 Like

Thank you. I think I understand.

Im probably attempting something out of my skill level. so im going to go do some more Tutorials and see if I stumble on to understanding this.

With the code you graciously gave me, I think ill be able to set which tiles are walls and which arent. From there I think I can have the Tilemap level be plugged in on load,
have the movement system “check” the tile in front of it to see if its a wall or not. if it isnt then move = true if there is move = false?
then use Layermask to check the layer tag? and make / tag things with a “blocking layer” tab
I feel like i have the idea, I just can not grasp what to type haha.

Thank you for your time.

Yeah, if you’re doing discrete tile by tile movement, you can do your movement checks without relying on the colliders. Colliders are actually kinda overkill for that sort of thing.

1 Like

LOL wow. I was thinking it was a the basic way to do things, that explains why I can found nothing on it.

so I am on the right track now?
with using Tilemap tilemap to get the grid, then
the Layermask to check the layers on the grid
then a if / if else statement added to my movement under something like “canmove” if true then canmove is executed?

Thank you so Derekloffin you really helped me out. spent a almost a week trying to figure out where I was going wrong.