# Angle towards Velocity

## Recommended Posts

Hi all,

I am having a little trouble trying to get my sprite to angle itself towards its current velocity. At the moment the zombies in my game are pointing to the top. They have a random velocity set in the code below. Is it possible to rotate these depending on the direction is it moving?

Here is the code I am currently using.

The enemies are spawning fine and moving in random directions, just trying to get the lads to rotate to their velocity is a pain.

``````createEnemies: function() {
for (var x = 0; x < startingEnemies; x++) { // Create 5 enemies with random positions
var enemy = enemies.create(this.game.rnd.integerInRange(50, 1870), this.game.rnd.integerInRange(50, this.game.world.height - 50), 'enemy');
enemy.body.velocity.setTo(this.game.rnd.integerInRange(50, 150), this.game.rnd.integerInRange(50, 150));
enemy.body.bounce.set(1);

enemy.body.collideWorldBounds = true;
enemy.animations.play('animationZombie', 30, true);

}
enemies.x = 100;
enemies.y = 50;
}``````

I am using Phaser v2.9.2 and the ARCADE physics.

This is my first game and post, so let me know if there is any other information you require.

Cheers!

##### Share on other sites

atan2 should give you the angle in radians, so you could do:

``enemy.rotation = Math.atan2(enemy.body.velocity.y,enemy.body.velocity.x) //rotation is in radians``

##### Share on other sites

If there's been at least one update, you can use

``enemy.rotation = enemy.body.angle;``

In the latest Phaser CE you can use

``enemy.rotation = enemy.body.velocity.atan();``

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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