Pave

Rotate Sprite to face a specific coordinate

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)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.