Camera follow sprite

Hi, I am making a game where camera follows sprite and the sprite is moving + rotating in the direction of pointer but when it reaches the end of canvas it stops moving forward and weirdly starts rotating. This is my code:

    update(){
         dx = this.input.activePointer.worldX - buggy.x.toFixed()
         dy = this.input.activePointer.worldY - buggy.y.toFixed()

         vec = Math.atan2(dy,dx)

        vx = Math.cos(vec) * speed
        vy = Math.sin(vec) * speed

       buggy.setVelocityX(vx)
       buggy.setVelocityY(vy)
    }

By default the input pointer coordinates never leave the game canvas. So the sprite is meeting the pointer near the edge of the canvas and then bouncing around in orbit.

I need the sprite to keep moving. I tried using Math.atan(dy/dx) instead and Phaser.Math.Angle.Between() and none seems to work. Although Math.atan() keeps the sprite moving forward out of canvas but only in one direction. Is there a proper way to do this?
I have seen examples on phaser lab but they’re mostly of cursor. I want my sprite to follow pointer.

Do you use matter phyiscs for moving around?
if not, try it out :slight_smile: Im using it in my game and set the sprite rotation + the thrust to move forward / back.
with the help of air friction the movement is stopped slowly :slight_smile:

see this example:

if you dont use the build in physics engine, u should try to update your “target” logic.
as u my see in my example:
i set the “speed” only once if the cursor moves ( “pointermove” => updateShip ).
Inside this method i set the new direction (rotate to the new world point ) and (if distance is far enough) set the new speed.
Ship keeps moving in this direction until cursor moved again. Ship is stopping if cursor moves over the ship.

in debug text u see all the relevant positions (world, ship, pointer, pointer-world)

:slight_smile:

Sorry I’m getting back to you so late. I tried the code according to my game and it worked. Thank you so much for you help!