Moving in a diamond - enemy gets stuck

Posted by Fibericon on Game Development See other posts from Game Development or by Fibericon
Published on 2012-10-07T11:07:37Z Indexed on 2012/10/07 15:50 UTC
Read the original article Hit count: 152

Filed under:
|

I have an enemy that I would like to move as follows:

  1. Start at (0, 200, 0)
  2. Move to (200, 0, 0)
  3. Move to (0, -200, 0)
  4. Move to (-200, 0, 0)
  5. Move to start point, repeat as long as it remains active.

This is what I've done to achieve that:

if (position.X < 200 && position.Y > 0)
{
   Velocity = new Vector3(1, -1, 0) * speed;
}
else if (position.X >= 200 && position.Y <= 0 && position.Y > -200)
{
   Velocity = new Vector3(-1, -1, 0) * speed;
}
else if (position.X <= 0 && position.Y <= -200)
{
   Velocity = new Vector3(-1, 1, 0) * speed;
}
else
{
   Velocity = new Vector3(1, 1, 0) * speed;
}

It moves to the second point, but then gets stuck and appears to vibrate in place. How should I be doing this?

© Game Development or respective owner

Related posts about XNA

Related posts about vector