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:

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(325600Phaser.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(playerPhaser.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.rotation200player.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.xplayer.body.y);
          bullet.rotation = player.rotation;
          game.physics.arcade.velocityFromRotation(player.rotation400bullet.body.velocity);
          bullet.body.velocity.y = -400;
          bulletTime = game.time.now + 50;
        }
    }
}

 

 

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

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

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

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...