Hello, so I have this problem with rotating the player by mouse when the world’s and camera’s bounds are set to (0, 0, 3200, 2400) and the camera is following the player.
The code below works, but the problem is, that when you move with the player and don’t move with the pointer, this.input.mousePointer.WorldX,Y doesn’t refresh automatically, making the rotation only correct when player moves with the mouse.
I even included this.input.setPollAlways(), but it doesn’t seem to work.
You can try the movement here: https://ogamrima.github.io/index.html
Place the pointer somewhere and try moving with the player (WASD) across the map and not touching the mouse, I want the player to always face towards the pointer.
class GamePlay extends Phaser.Scene {
constructor() {
super({
key: "GamePlay",
});
this.speed = 400;
this.w = 3200;
this.h = 2400;
this.mouseX = 0;
this.mouseY = 0;
}
preload() {}
create() {
this.background = this.add.image(this.w / 2, this.h / 2, "background");
this.player = this.physics.add.sprite(this.w / 2, this.h / 2, "player");
this.player.setScale(0.25).setCollideWorldBounds();
this.physics.world.setBounds(0, 0, this.w, this.h);
this.cameras.main.zoom = 0.5;
this.cameras.main.startFollow(this.player, true);
this.cameras.main.setBounds(0, 0, this.w, this.h);
this.input.setPollAlways();
this.txt = this.add.text(
this.cameras.main.scrollX,
this.cameras.main.scrollY,
"h ",
{
font: "25px Arial",
fill: "yellow",
}
);
this.moveKeys = this.input.keyboard.addKeys({
up: Phaser.Input.Keyboard.KeyCodes.W,
down: Phaser.Input.Keyboard.KeyCodes.S,
left: Phaser.Input.Keyboard.KeyCodes.A,
right: Phaser.Input.Keyboard.KeyCodes.D,
});
this.input.keyboard.on("keydown_W", (e) => {
this.player.setVelocityY(-this.speed);
});
this.input.keyboard.on("keydown_S", (e) => {
this.player.setVelocityY(this.speed);
});
this.input.keyboard.on("keydown_A", (e) => {
this.player.setVelocityX(-this.speed);
});
this.input.keyboard.on("keydown_D", (e) => {
this.player.setVelocityX(this.speed);
});
this.input.keyboard.on("keyup_W", (e) => {
if (this.moveKeys["down"].isUp) {
this.player.setVelocityY(0);
}
});
this.input.keyboard.on("keyup_S", (e) => {
if (this.moveKeys["up"].isUp) {
this.player.setVelocityY(0);
}
});
this.input.keyboard.on("keyup_A", (e) => {
if (this.moveKeys["right"].isUp) {
this.player.setVelocityX(0);
}
});
this.input.keyboard.on("keyup_D", (e) => {
if (this.moveKeys["left"].isUp) {
this.player.setVelocityX(0);
}
});
}
update(time, delta) {
this.mouseX = this.input.mousePointer.worldX;
this.mouseY = this.input.mousePointer.worldY;
this.player.rotation = Phaser.Math.Angle.Between(
this.player.x,
this.player.y,
this.mouseX,
this.mouseY
);
this.txt.text = this.mouseX + ", " + this.mouseY;
this.txt.setPosition(this.cameras.main.scrollX, this.cameras.main.scrollY);
}
}