How to allow tile collision only to the top?

What I wanted to achieve here is, I want the sprite to overlap tiles and only collide when the sprite is on top of tiles.

I can’t find a tutorial for this kind of stuff, so I am asking you all to aid and enlighten me.

I guess you could use setCollision().
See https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tile.html

1 Like

In this example (typescript) I iterate all map tiles (only once) and then assign a custom collide only on the tiles with the property customCollide true.
The property customCollide is a custom property assigned to certain tiles using Tiled editor.
this.map.forEachTile((tile: Phaser.Tilemaps.Tile) => {
if (tile.properties.customCollide != undefined) {
tile.setCollision(false, false, true, false, true);
}
}, this);

I have exacly same problem, but my character fall down from the map when i move him on the right. Anybody has fix for this? When tiles with ‘collideTop’ property are upper this works fine :confused:

image

Assuming you’re creating your platforms with Tiled Editor on a tile layer called “platofrms” and you’ve assigned the tile property name name “jumpThrough” for tiles that you want to be collid-able only from the top, something like this should work:

this.platforms = map.createLayer("platforms", null, 0, 0);
this.platforms.setCollisionByExclusion(-1, true);

this.platforms.forEachTile(tile => {
      if (tile.properties.name === "jumpThrough") {
        tile.setCollision(false, false, true, false);
      }
    });

It isn’t working. My player always fall down when I move. This is my code:

        this.map = this.add.tilemap('level2');
        let tileset = this.map.addTilesetImage('lvl2_sprites', 'tilesetNameInPhaser');
        this.floor = this.map.createStaticLayer('floor', [tileset], 0, 0);    
        // collision    
        this.physics.add.collider([this.player], [this.floor]);
        this.floor.setCollisionByExclusion(-1, true);
        this.floor.tilemap.forEachTile((tile) => {
            if (tile.properties.collidesTop) {
                tile.setCollision(false, false, true, false);
            }
        })