FlatBeatle Posted December 17, 2017 Share Posted December 17, 2017 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'); this.game.physics.enable(enemy, Phaser.Physics.ARCADE); 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; var animationZombie = enemy.animations.add('animationZombie'); 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! Link to comment Share on other sites More sharing options...
samid737 Posted December 18, 2017 Share Posted December 18, 2017 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 Link to comment Share on other sites More sharing options...
samme Posted December 18, 2017 Share Posted December 18, 2017 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(); kvazmatik 1 Link to comment Share on other sites More sharing options...
Recommended Posts