Jump to content

Changing projectile direction based on position


Recommended Posts

I have a prefab that shoots fireballs, and based on where it's at on the screen, it will shoot a fireball in a certain direction. The code below works except for the fact that if the object crosses the point where it changes the fireball's direction, the pre-existing fireball will change direction. The below code is in the update function. How do I prevent pre-existing fireballs from changing direction?

var xPosition = this.position.x;
var xDirection = xPosition >= this.game.world.centerX ? 1 : -1;

var yPosition = this.position.y;
var yDirection = yPosition <= this.game.world.centerY ? 1 : -1;

this.fireball.body.velocity.x = xDirection * 200;
this.fireball.body.velocity.y = yDirection * 200;
Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...