Jump to content

Can't get bullets to follow sprite rotation and shoot in the direction of the sprite

Recommended Posts

I want the bullets to fire in the direction the ship is facing such as in this example:



I have followed the same code structure but my bullets don't fire in the direction I'm facing. 


// Initialize the Phaser Game object and set default game window size
const game = new Phaser.Game(325600Phaser.CANVAS'', {
  preload: preload,
  create: create,
  update: update })
function preload () {
  //player assets 
//bullet variables
var bullets;
var bulletTime = 0;
var ebulletTime = 0;
function create () {
    //Player Variables ----------------------------------
    MAX_VELOCITY = 200;                 // pixels/second
    DRAG = 100;                         // pixels/second                
    playerbullets = game.add.group();
    playerbullets.enableBody = true;
    playerbullets.physicsBodyType = Phaser.Physics.ARCADE;
    player = game.add.sprite(game.width/2,game.height,'player',0);
    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;
  if (cursors.up.isDown){
      //player.animations.play('thrusters', 30, true);
    player.body.angularVelocity = -300;
  else if(cursors.right.isDown){
    player.body.angularVelocity = 300;
    player.body.angularVelocity = 0;
function PlayerBulletFired () {
    if (game.time.now > bulletTime)//adds a delay to the bullet time
        bullet = playerbullets.getFirstExists(false);
        if (bullet)
          bullet.rotation = player.rotation;
          bullet.body.velocity.y = -400;
          bulletTime = game.time.now + 50;



Link to post
Share on other sites
  • 4 weeks later...


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 by supertommy
update link
Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...