top of page
Search
  • cedarcantab

Danmaku using Phaser 3 (step10): Aiming at the Player!

Updated: Oct 8, 2021

Following on from bullets that bend, how about if the danmaku shot bullets in the direction of the player? And as the player moved, the direction in which the bullets fired tracked the player?


In this post, we will add a "target" parameter to the danmaku so that the angle of the primary cannon would "track" the "target".


In the fireweapon() method, we will call a to-be created method called aimAt(object) which would adjust the angle of the primary cannon (ie, danmaku.angle, or in radians, danmaku.rotation), in the direction of the player.


To calculate the direction to point the cannon at, we can use another one of Phaser's built in functions called Phaser.Math.Angle.Between(x1, y1, x2, y2) which calculates the angle between points (x1,y1) and (x2,y2), like so.


aimAt(target) {
    const targetAngle = Phaser.Math.Angle.Between(this.x, this.y, target.x, target.y);
    this.rotation = targetAngle
  }

Then we can call this from the fireweapon() function


fireweapon() {
    if (this.target) {this.aimAt(this.target)}

In the constructor function of the danmaku class, we add the line below to accept the new parameter.

this.target = config.target || null;

And that's it! As usual, the full CodePen Pen is available for your perusal here.





記事: Blog2_Post
bottom of page