I’m making a easy sport in Phaser (however the library does not actually matter).
I’ve an enemy that’s shifting in the direction of the participant on a 2 dimensional grid. I’ve written some code that makes the enemy transfer in the direction of the participant:
moveEnemy(enemy){
var playerX = getPlayerX(); // a quantity
var playerY = getPlayerY(); // a quantity
// if the enemy X or Y coordinate is smaller or bigger than the gamers,
// improve or lower the enemy X or Y to maneuver nearer.
var newX = enemy.x > playerX ? enemy.x - 0.2 : enemy.x + 0.2;
var newY = enemy.y > playerY ? enemy.y - 0.2 : enemy.y + 0.2;
enemy.setPosition(newX, newY);
}
I anticipated this code to maneuver the enemy in the direction of the participant within the shortest route doable. However, I get the next scenario:
The inexperienced route is what I anticipated the enemy to take. In actuality, the enemy follows the crimson line: first it goes down in a forty five diploma angle, earlier than going in the direction of the participant in a straight horizontal line. I’m at a loss methods to make my enemies transfer alongside the inexperienced line.