How to set collision layer of Tilemap in Godot

Godot Version

4.2

Question

I want to set the collision layer of the tilemap programmatically based on a variable. Image below will help:


This is my custom tilemap class, it is basically a normal tilemap at the moment but it has a variable elevation. I want to set the collision layer and mask of the tilemap to the elevation in my script. I can set the layer and mask in the editor, so surely it is possible in the script.

1 Like

Physics layers are part of the TileSet. You can change their values with Tileset.set_physics_layer_collision_layer() and TileSet.set_physics_layer_collision_mask() like:

extends Node

@onready var tile_map: TileMap = $TileMap

func _ready() -> void:
	await get_tree().create_timer(2).timeout
	tile_map.tile_set.set_physics_layer_collision_layer(0, 0)
2 Likes

Perfect, thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.