敵の船が常にプレイヤーに向かって移動するようにしています。プレイヤーの X 座標と Y 座標、および船の X 座標と Y 座標を指定して、現在、船をプレイヤーに向かって移動させるためにこれを実行しています。
Enemy.prototype.update = function(playerX, playerY) {
// Rotate the enemy to face the player
this.rotation = Math.atan2(this.y - playerY, this.x - playerX) - 2.35;
// Move in the direction we're facing
this.x += Math.sin(this.rotation) * this.speed;
this.y -= Math.cos(this.rotation) * this.speed;
}
これは機能しますが、敵は最終的に衝突するまでプレイヤーの周りを回っているように見えます。敵が常にプレイヤーに向かって一直線に移動するようにしたいだけです。
どうすればこれを達成できますか?