How do I make camera follow sprite and sprite follow mouse at the same time? [SOLVED]

Hey Guys. I’m new to this forum and also new to coding. I am currently having an issue where I can’t quite get the camera to follow the sprite and the sprite to follow the mouse to work at the same time.

First I made the sprite to follow the mouse and it works perfectly fine.

this.input.on('pointermove', function (pointer) {
      this.physics.moveToObject(this.player, pointer, 240);
    }, this);

Then when I added this code for the camera to follow the sprite, the sprite doesn’t follow the mouse correctly.
It follows the mouse but at a slightly off angle. If I point my mouse in the left direction, it would go to the North left direction instead. Sometimes it would go to the opposite direction of where I am pointing at. So it’s a little buggy.

this.cameras.main.startFollow(this.player);

Any suggestions on how I can make this work?

The issue, there, is that pointer will contain the coordinates x and y of the cursor but relative to the canvas (or window, depending on your game config).
This means that the player will not move in the same referential as the cursor and it will cause a very strange behavior. You simply have to convert your canvas coordinates to world coordinates.

The following code may do the trick but I haven’t actually used getWorldPoint. I just took it from the docs.

const transformedPoint = this.cameras.main.getWorldPoint(pointer.x, pointer.y);
this.physics.moveToObject(this.player, transformedPoint, 240);
2 Likes

Not sure if this helps, but I think converting the coordinates yourself is not necessary. The pointer returns worldX and worldY in addition to x and y. They will tell you the coordinates in the world space

2 Likes

hmmm…it didn’t work. It give me an error saying the “pointer is not defined”. So I did this instead
but it still give me the same behavior as last time.

this.cameras.main.startFollow(player);

const transformedPoint = this.cameras.main.getWorldPoint(this.input.x, this.input.y);

  this.input.on('pointermove', function (transformedPoint) {
    this.physics.moveToObject(player, transformedPoint, 240);
    }, this);

Sorry for not being clear enough.
The pointer variable is the one you get inside your "pointermove" event.
That the point you have to use.
From the code you just gave, try this:

this.cameras.main.startFollow(player);

this.input.on('pointermove', function (pointer) {
    this.physics.moveToObject(player, {x: pointer.worldX, y: pointer.worldY}, 240);
}, this);

That’s not very clean code but I hope it will help you get the idea!

1 Like
this.input.on('pointermove', function (pointer) {
    const transformedPoint = this.cameras.main.getWorldPoint(pointer.x, pointer.y);
    this.physics.moveToObject(player, transformedPoint, 240);
    }, this);

Sorry for not understanding this lol. I am very much a noob. It worked. Thank you so much for your help.
So what exactly is world coordinates? Whats the difference between canvas and world? Sorry I just wanna understand this logically. :grimacing:

I used my Paint skills to help you better!

Ignore the text on the top left saying “lever” and “tp_stairs_to_forest”, it’s something for my game.

The whole map is pretty big, too big to fit on screen at once. The camera moves around it and the red rectangle represents what we see of the world through the camera.
The green cross is the point we clicked on.
I drew two lines to let you see what x/y and worldX/worldY really represent for Phaser.

The yellow lines show what the coordinates of the point are. We can clearly see that they start from the camera screen.
On the other hand, the blue lines start from the borders of the whole map.
Your character doesn’t care about the camera, it simply moves through the world and that’s why you have to use the blue lines to really tell him where to go.

In my previous post, I used the word canvas to speak about this red rectangle.
The canvas is the rectangle/square you draw your whole game with. It’s not very important to understand it so don’t worry too much about it! Sorry for bringing it!

2 Likes

Oh I see now. The pointer.x and pointer.y is only used inside the camera view but in order for the character to move properly(if you’re going outside of the camera border), you have to use pointer.worldX and pointer.worldY.
Got it! Thank you so much for helping me. Happy New Years! :smile:

Even if you’re moving inside the camera border, you have to use the world coordinates.
Otherwise, the game will think that the red rectangle is on the top left of the screen and your character will always want to go to the top left where it thinks you’ve clicked.

1 Like

Got it! I will keep that in mind.

Hi Pros,

Can we restrict the movement of player to the cursor pointer only on x axis. And camera follows player.