Unwanted collisions between players when loading tilemap

Hello, thanks for taking the time to read this. I’m working on my first game in Phaser. I created a couple of players and a scene where both players will fight some enemies. I created a physics group for the players and a static layer for my tileset. But when I add the collider between the players and the ground layer, it also adds a collider between the players, and I don’t want that. Any suggestions?

this.players = this.physics.add.group();

this.player0 = new Player(this, 128, 32);
this.player1 = new Player(this, 64, 32);

this.players.add(this.player0);
this.players.add(this.player1);

this.map = this.make.tilemap({ key: ‘map1’ });
this.groundTiles = this.map.addTilesetImage(‘Tile_sheet’, ‘tileSheet’);
this.groundLayer = this.map.createStaticLayer(‘Ground’, this.groundTiles, 0, 0);

this.groundLayer.setCollisionBetween(1, 29);

this.physics.add.collider(this.players, this.groundLayer);

You mean the players are colliding with each other? Are you sure?

1 Like

100% sure. If player0 runs into player1, player0 slows down and player1 is pushed. And if both of them run in opposite directions they stop.

The players also have a collider with this group of platforms, and deleting this fixes the problem. Am I doing something wrong here??

this.platforms = this.physics.add.staticGroup();
this.platforms.create(366, 166, ‘platform’);
this.platforms.create(400, 102, ‘platform’);

this.physics.add.collider(this.players, this.platforms);

It looks correct.

What happens if you remove the groundLayer collider?

1 Like

I just fixed it! When I copied the code I didn’t include a few things because I thought it wasn’t relevant, but it was. Basically when I did this:

this.physics.add.collider(this.players, this.platforms);

Platforms was undefined, so it added a collider between the players. Makes so much sense… Thanks for your time though!