FlatBeatle 0 Posted December 17, 2017 Report 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! Quote Link to post Share on other sites
samid737 118 Posted December 18, 2017 Report 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 Quote Link to post Share on other sites
samme 722 Posted December 18, 2017 Report 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 Quote Link to post Share on other sites
Recommended Posts
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.