So, I'm making a game where you are to outrun enemies. Instead of having the enemies move at only up, down, left, right, and at 45 degree angles, I want the enemy to take the shortest linear path towards the player. Here's 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 this does is sets the velocity in cardinal directions. What could I do to make this more accurate?