Jump to content

Rotate Sprite to face a specific coordinate


Pave
 Share

Recommended Posts

I am trying to move my Arcade Sprite to a specific point (let's say it's (50|15) and the sprite is currently at (300|500)). Therefore I am using the moveTo() function.

 

The movement of the object works but the direction it is facing is completely wrong. So how can I let my sprite rotate to this point? (with a short transition would be very nice)

Link to comment
Share on other sites

@samme thanks for the correct link. Now I found the Math function BetweenPoints().

Unfortunately I can't get it to work.

Here is part of my code:
 

class Enemy extends Phaser.Physics.Arcade.Sprite {

	constructor (scene, x,y)
    {
        super(scene, x,y);
    }
	
    preUpdate (time, delta)
    {
		this.rotateFunc();
    }
    
    rotateFunc(){
        let currentPoint = new Phaser.Geom.Point(this.x,this.y+this.displayHeight/2);
        let pointToMoveTo = new Phaser.Geom.Point(Player.players.getChildren()[0].x, Player.players.getChildren()[0].y);
        this.rotation = Phaser.Math.Angle.BetweenPoints(currentPoint, pointToMoveTo);
        this.arcadePhysics.moveTo(this, Player.players.getChildren()[0].x, Player.players.getChildren()[0].y, 30);
    }
}	

 

In this example I want to rotate the enemy sprite to face the player sprite. For the error see the attached video.

Link to comment
Share on other sites

  • 2 years later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...