I'm a beginner in Phaser 3 and I'm facing an issue with my character's jumping mechanic.
Here is the code snippet I'm currently using:
create() {
this.player = this.physics.add.sprite(50, 380, 'idle');
this.player.setScale(.3);
this.player.setGravityY(300);
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
this.physics.add.collider(this.player, this.platforms);
this.cursorKeys = this.input.keyboard.createCursorKeys()
}
update() {
this.movePlayer()
}
movePlayer() {
if (this.cursorKeys.left.isDown) {
this.player.setVelocityX(-300)
} else if (this.cursorKeys.right.isDown) {
this.player.setVelocityX(300)
} else {
this.player.setVelocityX(0);
this.player.setVelocityY(0);
}
if (this.cursorKeys.up.isDown && this.player.body.touching.down) {
this.player.setVelocityY(-300);
}
}
Currently, my character only jumps a few pixels up and then stops. If I remove the touching.down
condition, the player can jump freely but the jump feels slow and floaty regardless of the gravity settings.
Any suggestions or solutions to improve the jumping behavior would be greatly appreciated!