I have a Enemy class that Seeks a player if he is in his field of view. Problem is i want the enemy only to move up/down/right/left, not like for example up and left at the same time (diagonal movement).
EDIT: Code updated
private void SeekPlayer(GameTime gameTime, Player player)
{
directionToPlayer = player.Image.Position - Image.Position;
directionToPlayer.Normalize();
if(Image.Position != player.Image.Position)
{
if (directionToPlayer.X != player.Image.Position.X)
{
if(directionToPlayer.X < player.Image.Position.X)
Image.Position.X += directionToPlayer.X * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
Image.Position.X -= directionToPlayer.X * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (directionToPlayer.Y != player.Image.Position.Y)
{
if (directionToPlayer.Y < player.Image.Position.Y)
Image.Position.Y += directionToPlayer.Y * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
Image.Position.Y -= directionToPlayer.Y * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
}
UpdateFOV();
}
With the current code the enemy sometimes follows the play on the X coordinates, and when it's the same has the player's, doesn't continue to follow on the Y coordinate. And vice-versa for Y.
X
first \$\endgroup\$