# Player pushes objects through walls

**URL:** <https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863>\
**Category:** Phaser 3\
**Created:** [June 29, 2021, 6:01am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863 "2021-06-29T06:01:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 6:01am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/1 "2021-06-29T06:01:56Z")

</div>

I have a crate object and I don’t set crate.body.immovable = true, so the player can push it. When it reaches a wall, it goes through. I put the following in the update (only trying from the left side for now)

```
if (crate.body.touching.left || crate.body.blocked.left){
  crate.setVelocityX(0)
  crate.body.immovable = true}

```

It still passes through the wall. How should I check whether the crate has touched a wall?

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 29, 2021, 6:41am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/2 "2021-06-29T06:41:20Z")

</div>

Add a collider for wall/crate?

---

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 8:07am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/3 "2021-06-29T08:07:30Z")

</div>

The wall is made of tiles from the world layer. It already has a collision

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 29, 2021, 8:20am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/4 "2021-06-29T08:20:21Z")

</div>

> [@t\_rev44](#):
>
> It already has a collision

With what? Not with the crate it seems. You should be able to add as many colliders as you want.

---

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 8:30am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/5 "2021-06-29T08:30:24Z")

</div>

They have a collider because otherwise the crate would fall through the floor. The floor and wall are the same thing, both made from tiles in the map.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 29, 2021, 8:37am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/6 "2021-06-29T08:37:26Z")

</div>

Interesting. Can you create an example?

You could try increasing TILE\_BIAS, but I can’t imagine you pushing that hard 🙂

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [June 29, 2021, 12:58pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/7 "2021-06-29T12:58:09Z")

</div>

Perhaps this, not tried:

```javascript
scene.physics.add.collider(player, crate, undefined, (player, crate) =>
{
  if (crate.body.touching.left || crate.body.blocked.left) {
    crate.body.immovable = true;
    return true;
  }
}, scene)

```

---

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 6:41pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/8 "2021-06-29T18:41:14Z")

</div>

That was a good suggestion, but it still goes thru the wall

 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/5/5883cf598320aacbc090e1b1e9012b9ff978d549.png)

 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/2/27136144d75188eefecbfe3805591133e6d44ca3.png)

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 29, 2021, 6:43pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/9 "2021-06-29T18:43:42Z")

</div>

> [@t\_rev44](#):
>
> The wall is made of tiles from the world layer. It already has a collision

You should set `crate.body.immovable = true` in the callback for this collider.

---

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 6:46pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/10 "2021-06-29T18:46:14Z")

</div>

If I do that, it can’t move at all because it’s always touching the floor.

At this point, my idea is to just manually plug in a property on the crate with an x coordinate it can’t move past.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 29, 2021, 6:50pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/11 "2021-06-29T18:50:15Z")

</div>

This makes no sense. You’re trying to work around a problem that shouldn’t be there. You should not be able to push through the tile. Focus on that.  
I notice the floor and wall are different tiles and sizes, so different layers?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 29, 2021, 6:53pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/12 "2021-06-29T18:53:03Z")

</div>

> [@t\_rev44](#):
>
> If I do that, it can’t move at all because it’s always touching the floor.

Only for `blocked.left`, `blocked.right`.

Well, maybe you should check `block.body.blocked` in the player–block collider callback instead. Must run after the block–tiles collider.

---

<div class="post-metadata">

**Author:** ![t\_rev44](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/t_rev44/32/4157_2.png) [@t\_rev44](https://phaser.discourse.group/u/t_rev44)\
**Post date:** [June 29, 2021, 7:06pm UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/13 "2021-06-29T19:06:21Z")

</div>

That was one of the first things I tried. I logged crate.body.blocked.left in the update and it’s always false, even as it passes thru the wall.

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [June 30, 2021, 8:27am UTC](https://phaser.discourse.group/t/player-pushes-objects-through-walls/9863/14 "2021-06-30T08:27:43Z")

</div>

I just tested with phaser 3 sandbox, seems to works with nothing special.

Here the code, based on this code: [Phaser 3 Examples](https://labs.phaser.io/edit.html?src=src/tilemap/collision/tile%20callbacks.js&v=3.55.2)

```javascript
var config = {
    type: Phaser.WEBGL,
    width: 800,
    height: 576,
    backgroundColor: '#2d2d2d',
    parent: 'phaser-example',
    pixelArt: true,
    physics: {
        default: 'arcade',
        arcade: { gravity: { y: 300 } }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);
var map;
var cursors;
var player;
var crate;
var groundLayer;

function preload ()
{
    this.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
    this.load.spritesheet('coin', 'assets/sprites/coin.png', { frameWidth: 32, frameHeight: 32 });
    this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/tile-collision-test.json');
    this.load.image('player', 'assets/sprites/phaser-dude.png');
}

function create ()
{
    map = this.make.tilemap({ key: 'map' });
    var groundTiles = map.addTilesetImage('ground_1x1');

    map.createLayer('Background Layer', groundTiles, 0, 0);
    groundLayer = map.createLayer('Ground Layer', groundTiles, 0, 0);

    groundLayer.setCollisionBetween(1, 25);

    player = this.physics.add.sprite(120, 520, 'player');

    crate = this.physics.add.sprite(80, 520, 'coin')

    this.physics.add.collider(player, groundLayer);
    this.physics.add.collider(crate, groundLayer);
    this.physics.add.collider(player, crate);

    this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
    this.cameras.main.startFollow(player);

    cursors = this.input.keyboard.createCursorKeys();
}

function update (time, delta)
{
    // Horizontal movement
    player.body.setVelocityX(0);

    if (cursors.left.isDown)
    {
        player.body.setVelocityX(-200);
    }
    else if (cursors.right.isDown)
    {
        player.body.setVelocityX(200);
    }

    // Jumping
    if ((cursors.space.isDown || cursors.up.isDown) && player.body.onFloor())
    {
        player.body.setVelocityY(-300);
    }
}

```
