callidus Posted February 22, 2020 Share Posted February 22, 2020 I want the bullets to fire in the direction the ship is facing such as in this example: https://phaser.io/examples/v2/arcade-physics/asteroids-movement I have followed the same code structure but my bullets don't fire in the direction I'm facing. Code: // Initialize the Phaser Game object and set default game window size const game = new Phaser.Game(325, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update }) function preload () { //player assets game.load.spritesheet('player', 'assets/player_spaceship/tempShipSpsritesheet.png',80,60,2); game.load.image("bullet", 'assets/bullet.png'); } //bullet variables var bullets; var bulletTime = 0; var ebulletTime = 0; function create () { //Player Variables ---------------------------------- MAX_VELOCITY = 200; // pixels/second DRAG = 100; // pixels/second game.physics.startSystem(Phaser.Physics.ARCADE); playerbullets = game.add.group(); playerbullets.enableBody = true; playerbullets.physicsBodyType = Phaser.Physics.ARCADE; playerbullets.createMultiple(10000, 'bullet'); playerbullets.setAll('anchor.x', 0.5); playerbullets.setAll('anchor.y', 0.5); playerbullets.setAll('outOfBoundsKill', true); playerbullets.setAll('checkWorldBounds', true); player = game.add.sprite(game.width/2,game.height,'player',0); game.physics.enable(player, Phaser.Physics.ARCADE); player.body.maxVelocity.set(MAX_VELOCITY) player.body.drag.set(DRAG) player.anchor.set(0.5); player.body.collideWorldBounds = true; //Environment -------------------------------- game.stage.backgroundColor = "#4488AA" //Controls ----------------------------------- cursors = game.input.keyboard.createCursorKeys(); game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]); } function update () { //Centers screen game.scale.pageAlignHorizontally = true; game.scale.pageAlignVertically = true; game.scale.refresh(); if (cursors.up.isDown){ game.physics.arcade.accelerationFromRotation(player.rotation, 200, player.body.acceleration); //player.animations.play('thrusters', 30, true); } else{ player.body.acceleration.set(0); } if(cursors.left.isDown){ player.body.angularVelocity = -300; } else if(cursors.right.isDown){ player.body.angularVelocity = 300; } else{ player.body.angularVelocity = 0; } if(game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)){ PlayerBulletFired() } } function PlayerBulletFired () { if (game.time.now > bulletTime)//adds a delay to the bullet time { bullet = playerbullets.getFirstExists(false); if (bullet) { bullet.scale.setTo(0.2,0.2); bullet.reset(player.body.x, player.body.y); bullet.rotation = player.rotation; game.physics.arcade.velocityFromRotation(player.rotation, 400, bullet.body.velocity); bullet.body.velocity.y = -400; bulletTime = game.time.now + 50; } } } Link to comment Share on other sites More sharing options...
supertommy Posted March 16, 2020 Share Posted March 16, 2020 (edited) @callidus using velocityFromRotation looks right but what is the line below it for? // this looks right game.physics.arcade.velocityFromRotation(player.rotation, 400, bullet.body.velocity); // what is this for? bullet.body.velocity.y = -400; I don't think you need that second line. I have an isolated demonstration of the concept here if that might help: https://blog.ourcade.co/posts/2020/fire-bullets-from-facing-direction-phaser-3/ Edited March 22, 2020 by supertommy update link callidus 1 Link to comment Share on other sites More sharing options...
callidus Posted March 19, 2020 Author Share Posted March 19, 2020 Thank you this helped solve my issue Link to comment Share on other sites More sharing options...
Recommended Posts