Tom Sykes
Tom Sykes

Reputation: 161

How to make enemies follow the player all around the screen in XNA?

Okay, so I've got some code to make my enemies follow the player in my XNA game, but they only follow the player until the player is in front of them. If the player moves past an enemy, it will stop moving towards him. Instead they will continuously move up and down with the player.

The code I've used is this:

         Vector2 direction = player.Position - goblins[i].Position;
         direction.Normalize();
         Vector2 velocity = direction * goblins[i].enemyMoveSpeed;
         goblins[i].Position += velocity;

(ignore the goblins bit, I've just replaced the graphics)

Not entirely sure where to go with it, any ideas?

Upvotes: 3

Views: 4970

Answers (1)

Vackup
Vackup

Reputation: 652

Tom, hello, how are you?

Here you have 2 examples that helped me a lot:

  1. Chase & Evade (http://xbox.create.msdn.com/en-US/education/catalog/sample/chase_evade) This is a Microsoft sample that shows how to implement several simple behaviors for AI, including chasing, evading, and wandering.
  2. Adding a field of view to enemies (http://robotfootgames.com/xna-tutorials/5-xna-platformer-starter-kit-field-of-view-for-enemies) Related to number 1 sample and to Plaformer starter kit

Upvotes: 2

Related Questions