Angle From Velocity
Align a sprite's rotation with its current movement direction by mapping its rotation property to the physics body's velocity angle.
800 × 600
class Example extends Phaser.Scene
{
arrow;
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('arrow', 'assets/sprites/arrow.png');
}
create ()
{
this.arrow = this.physics.add.image(400, 100, 'arrow')
.setVelocity(200, 0)
.setBounce(1, 1)
.setCollideWorldBounds(true);
}
update ()
{
this.arrow.rotation = this.arrow.body.angle;
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'game-container',
physics: {
default: 'arcade',
arcade: {
debug: false,
gravity: { y: 600 }
}
},
scene: Example
};
const game = new Phaser.Game(config);