Mouse pointer coordinates don't update

Hi all,
I’m facing a problem relative to mouse pointer: the worldX and worldY coordinates don’t update if I don’t move the mouse and only move the camera. The game I’m developing is a top-down shooter and the camera follow the player that is always at the center of the screen. As said before, if I only move the camera but not the mouse, the worldX and worldY coordinates of the mouse pointer don’t change but if I only click a button of the mouse, the coordinates will update correctly. I suspect that the worldX and worldY coordinates are updated only on mouse event (move and click) and not during every frame. Am I correct? How can I get the mouse coordinates relative to world even if the mouse is not moving (or clicking) but only the camera?

Thanks in advance

1 Like

https://labs.phaser.io/view.html?src=src/input\mouse\poll%20always.js

I had the same issue but fixed it with another solution.

My update() was:

this.rotation = Phaser.Math.Angle.Between(this.x, this.y, this.scene.input.mousePointer.worldX, this.scene.input.mousePointer.worldY)

The worldX and worldY were not updated on camera movement unless I moved my pointer. I was able to fix it by using the regular x/y and combining it with the camera x/y.

const crosshairX = this.scene.input.mousePointer.x + this.scene.cameras.main.worldView.x
const crosshairY = this.scene.input.mousePointer.y + this.scene.cameras.main.worldView.y
this.rotation = Phaser.Math.Angle.Between(this.x, this.y, crosshairX, crosshairY)

I don’t know if it is better then setPollAlways though…

1 Like

For anyone stumbling on this setPollAlways doesn’t work, here’s how to solve this -

getMouseCoords() {
    // Takes a Camera and updates this Pointer's worldX and worldY values so they are the result of a translation through the given Camera.
    this.scene.input.activePointer.updateWorldPoint(this.scene.cameras.main);
    const pointer = this.scene.input.activePointer
    return {
      mouseX: pointer.worldX,
      mouseY: pointer.worldY,
    }
  }

Found this issue which helped - setPollAlways() does nothing. InputManager only updates on DOM event callback. · Issue #4405 · photonstorm/phaser · GitHub

1 Like