phaser-frameworkboxhitphaserjs

Phaser Hit boxes are invisibly to the right, what gives?


I'm working on a game where the player can interact with stuff around them, like npcs, items, and examinable things in the environment. They can interact with stuff in 2 ways, pressing T to examine or talk to it or R to use or pick it up.

For some reason, the hitboxes that tell when the player character is in contact with something and thus should or should not be able to interact with it are off. More specifically, they seem to invisibly be moved to the right, thus demanding the player character to be on the right side of the hitboxes in order to interact with them.

Here is a picture of the player character trying to examine a door. As they are on the left side of the hitbox, when the player presses T, nothing happens. enter image description here

In this picture, the player character is on the right side of the hitbox, and thus the dialogue that is displayed when pressing T is shown. enter image description here

Finally, here is the player character, outside of the door's hitbox and still able to trigger the dialogue by pressing T. Clear proof that the hitboxes are invisibly moved to the right. enter image description here

Here is the code that creates the door.

this.doorSide = this.physics.add.sprite(548, 735, 'sideDoor');
    this.doorSide.body.immovable = true;
    this.doorSide.body.allowGravity = false;

And here is the code that triggers when the player presses T in the door's hitbox. It's same for all other interactables in the game.

if ((this.checkCollision(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
        this.talking = !this.talking;
    }

I really can't tell what went wrong or why this happens. Does anyone know how this happened and if there is a solution?

If it helps, I'm using Phaser 3 is VSCode employing arcade physics.


Solution

  • I assume the problem is in the function call this.checkCollision(...), but since you didn't share that code I just can assume.

    Nevertheless, if that function is the problem, the solution is, touse the builtin function overlap of phaser to check for collision/overlaps. (link to the documenation)

    if ((this.physics.overlap(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
        this.talking = !this.talking;
    }
    

    Tipp: it is always better to use builtin functions, when possible, instead of coding everything yourself.