Collision defined in tileset not working

Godot Version

4.3 stable Windows 11

Question

I have a CharacterBody2D with collision layer 2 and collision masks 1, 3, 4, 5, and 9, which is the parent of a CollisionShape2D. In the root node, I have a TileMapLayer with a TileSet with tiles in two physics layers, one having collision layer 1 with mask 2, 6 and another having collision layer 9 with mask 2, 3, 6.

My CharacterBody2D runs this code in _process():

var x_mov = Input.get_action_strength("right") - Input.get_action_strength("left")
	var y_mov = Input.get_action_strength("down") - Input.get_action_strength("up")
	var mov = Vector2(x_mov, y_mov)
	velocity = mov.normalized()*movement_speed
	move_and_slide()

The movement works fine, and what should be happening is the CharacterBody2D should be colliding with tiles in my tilemap that have the correct physics layers, but it just passes through all the tiles when I test it. The collision shapes are all correct and I can see them when enabled.

I’m not sure why this is happening, and I couldn’t find anything to help - every tutorial I see on tilemap collision does apparently the same thing I’m doing and just works. What is the problem here? Any help is appreciated.

Are the collisionmask-/layer also setup in the scene you are testing. When you first put your character in there and then edit them in the character-scene it wont update in the other scene

Did you paint out the collisions?

Indeed, the CharacterBody2D’s collision layers are set up in the scene itself. In fact, its collisions work for everything but the tilemap.

As far as I can tell, they are painted.

Is your node a tilemap or tilemap layer? I think it matters if its tilemap or tilemapLayer, to change it to a layer just press the toolbox and choose something something layers, once you have changed it to a layer no yellow exclamation mark will be there, and i dont know if this is right but i think your tile size has to be 16x16? Im not sure

What if you move the movement code to _physics_process instead of _process?

It’s a TileMapLayer, sorry, I didn’t know there was a difference. The yellow exclamation mark is just “The current atlas source has tiles outside the texture”, which I don’t believe is relevant.

1 Like

My apologies, it’s already on physics process, not process.

Can you show the node hierarchy? Is the player on a different CanvasLayer than the TileMapLayer?

(Also, I don’t think using a TileMap vs. a TileMapLayer will make a difference since the engine will actually make a TileMapLayer when running the game for you if you use a TileMap.)


I’m not sure what you mean by CanvasLayer, looking it up tells me that it’s a type of node and I’m not using any of those, but if you mean Z index, both CharacterBody2D and TileMapLayer have a Z index of -1.

(also, just for clarification, Player’s StageCollision is supposed to be what collides with the TileMapLayer)

So they are also set up for your “stage”-scene?

My stage scene is a Node2D and does not have collision layers. Should I change it to something else?

I think they are referring the the instance of the player in the stage scene, not the stage node itself.

I see the problem now - the player instance in the stage scene was missing the masks to collide with the tilemap, I added those and it fixed it. I’m not sure why I needed to separately define the collision layers in an instance of my player node considering I already set them in the player scene - I mean, I don’t remember doing this for all the other collision layers - but either way, thank you!

2 Likes

It’s because if you don’t change a property in the instance, it will update with the original scene, but if you do change it in the instance, it won’t update with the original scene. You must have changed the masks in the instance at one point.

This happened to me a few days ago. as @paintsimmon said if you already have an instance in another scene and update the original it wont update the instance in the seperate scene