Platformer Player Ducking

Hello amigos, here the problem I’m having:

I have this game where the player can duck and continue moving while doing so:

This is the normal state:

When ducking (down arrow) the bounding box is reduced to half:

When this happens, the player can still walk but now it can pass under certain platforms:

image

The problem I have is that once under the platform, I don’t want the player to return to it’s standing position, but this happens when I release the down arrow and the player goes through the platform:

image

While ducking, i tried checking player.body.blocked.up and player.body.touching.up but this does not work since this is not touching or being blocked, it is exactly one pixel below, so not really being blocked or touching.

Is there a way to test if it “will touch” this ceiling (the upper plaform).

Thanks a lot!

I guess you should manually check if there is a platform above the ape.

Have you used tiled to build the scene?

1 Like

Hey man, thanks for the reply.
Yes I used Tiled, not completely sure how to check if there is an specific tile above the character, any idea on how to do it?

Hi @webchimp,
You can use this method to check if there is a tile in a specific position:
https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tilemap.html#hasTileAtWorldXY__anchor
Regards.

2 Likes

@jjcapellan This is the answer!

Yes, here the way I solved the problem with @jjcapellan answer:

My platforms exist in a layer called groundLayer.
What I’m doing here is this:

function playerDucking() {

	walkingSpeed = 100;
	player.body.setSize(64, 32, false).setOffset(0, 32);

	if(!player.body.onFloor()) {

		state = states.FALLING;
	}

	if(cursors.down.isUp && !(groundLayer.hasTileAtWorldXY(player.body.position.x, player.body.position.y-1) || groundLayer.hasTileAtWorldXY(player.body.position.x+player.body.width, player.body.position.y-1))) {

		walkingSpeed = 200;
		player.body.setSize(64, 64, false).setOffset(0, 0);
		state = states.ON_GROUND;
	}
}

So, I check if the player has a tile above it, from the starting point of the sprite to the last pixel.
If so then the user can’t stand up.

Here the result:
image

Thanks a lot again.

1 Like