So, I am making a game in which you must be ahead of the enemies. Instead of the enemies moving only up, down, left, right and at an angle of 45 degrees, I want the enemy to take the shortest linear path to the player. Here is my code:
public void moveEnemy() {
if (player.pos.x > enemy.pos.x) {
enemy.vel.x = 3;
}
if (player.pos.x < enemy.pos.x) {
enemy.vel.x = -3;
}
if (player.pos.y > enemy.pos.y) {
enemy.vel.y = 3;
}
if (player.pos.y < enemy.pos.y) {
enemy.vel.y = -3;
}
if (player.pos.x == enemy.pos.x) {
enemy.vel.x = 0;
}
if (player.pos.y == enemy.pos.y) {
enemy.vel.y = 0;
}
}
So, what does it mean, sets the speed in cardinal directions. What can I do to make this more accurate?
source
share