0
\$\begingroup\$

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.

Gameplay

\$\endgroup\$
3
  • \$\begingroup\$ Do you want to be moved on X or Y first ? \$\endgroup\$
    – dimitris93
    Jun 27, 2015 at 17:17
  • \$\begingroup\$ I want X first \$\endgroup\$
    – xRed
    Jun 27, 2015 at 17:17
  • \$\begingroup\$ What is your question? Looks like it's doing exactly what you've specified that you want it to do? \$\endgroup\$ Jun 28, 2015 at 5:11

1 Answer 1

1
\$\begingroup\$

Your code has some serious issues. First of all, in games, you generally don't want to be moving objects on the screen by a flat amount per frame, because then the frame rate can make you go faster or slower, which is not a good thing. The way you want to be moving your objects is with delta time, which is the elapsed time between this and the previous Update() call.

As far as 'moving to target location' goes, you will need something better than a magical hard-coded number 0.1f. Check out this question, on how to move an object from one point to another, in XNA/Monogame.

Looking at your code, I don't see any reason for your directionToPlayer and Velocity variables to not be declared inside your SeekPlayer() function, so I declared them there. Also, you have the exact same code in an if-else statement. There is no point on doing that, since it is going to get called either way. So you might as well call it after the if-else statement. And finally, there is no point in using return; as the final command of a void function. It is going to return anyway.

float speed = 2;
Vector2 velocity = player.Image.Position - Image.Position;
velocity.Normalize();

if(velocity.X > 0.1f) // still not close enough on X-axis
    Image.Position.X += speed; // move on X-axis
else if(velocity.Y > 0.1f) // still not close enough on Y-axis
    Image.Position.Y += speed;  // move on Y-axis
UpdateFOV();
\$\endgroup\$
7
  • \$\begingroup\$ The Velocityvariable is not declared inside the SeekPlayer()because the enemy will move (wander) if the player is not im his FOV. So i updated my code, and now the problem is that enemy.X is never equals no player.X. It goes to the infite.. 0,000(0)1... Making the enemy following the player on the X axis \$\endgroup\$
    – xRed
    Jun 27, 2015 at 18:41
  • \$\begingroup\$ Why multiply with the velocity? That will move the enemy on the axes with different speed, in relation to the angle to the player. It should be Image.Position.X|Y += speed * deltaTime; \$\endgroup\$ Jun 27, 2015 at 18:44
  • \$\begingroup\$ @xRed Of course the enemy.X won't be equal to player.X (most of the time). Lets say you have values player.X = 500 and enemy.X = 300 and you increase the enemy.X by 3 every frame. Who guarantees you that it will reach exactly the value 500 ? In this specific case it won't. This is one of the reasons why your code as a whole is bad. You can check the question I linked in my question for a completely different approach. \$\endgroup\$
    – dimitris93
    Jun 27, 2015 at 18:55
  • \$\begingroup\$ @Shiro That will still result in different velocities for X/Y most of the time. If you normalize (3|1), than you will get X=3/sqrt(10) and Y=1/sqrt(10). \$\endgroup\$ Jun 27, 2015 at 18:58
  • \$\begingroup\$ @user000user Oh yes, my bad, you are correct. It was presented like this in the original question and I didn't realize this could happen. \$\endgroup\$
    – dimitris93
    Jun 27, 2015 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .