0

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.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
EmptyStone
  • 235
  • 1
  • 7

1 Answers1

0

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.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • My bad, I meant to add the code in checkCollision. Here it is, hope your can read it. – EmptyStone Jul 03 '23 at 05:30
  • checkCollision(a, b) { // simple AABB checking if ((a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.height + a.y > b.y) ) { return true; } else { return false; } } – EmptyStone Jul 03 '23 at 05:30
  • @EmptyStone thanks for sharing your code, but I still suggest using the the builtin function as mentioned in my answer. If that doesn't solve the problem, the problem might be somewhere else. **P.s.:** btw you can update your question and post your code there. – winner_joiner Jul 03 '23 at 05:55
  • Did you try using the code from my answer? – winner_joiner Jul 03 '23 at 05:55
  • Ok, this overlap thing appears to have worked. Now I have a lot of collisionChecks to replace. One question though, from what I can tell, this only works with the aracde physics, right? – EmptyStone Jul 03 '23 at 06:01
  • It works shoule also with matter physics ([link to the documentation](https://newdocs.phaser.io/docs/3.55.2/focus/Phaser.Physics.Matter.MatterPhysics-overlap)), but if you don't configure any physics in the Phaser config, it won't work. – winner_joiner Jul 03 '23 at 06:05