I have one tower and one enemy. the tower job is to shoot bullets at the enemy and each bullet will follow the enemy until it hits him.
However the problem is when the enemy is faster than the bullet, the bullet misses the enemy and tries to him the enemy but can't and it will go off screen.
this video will help you understand more.
[media]
[/media]
look at 0:05 and 0:08 and 0:09 see how the bullet misses the enemy and tries to hit him and can't.
So what i want is for the bullet to keep following the enemy no matter what until it hits him.
class Bullet
{
void CalculateBulletPosition()
{
DeltaPosition.X = Enemy.EnemyRectangle.X - Tower.Position.X;
DeltaPosition.Y = Enemy.EnemyRectangle.Y - Tower.Position.Y;
double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));
Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}
public void Update()
{
CalculateBulletPosition();
Velocity.X += Direction.X * Speed;
Velocity.Y += Direction.Y * Speed;
BulletRectangle = new Rectangle((int)(Velocity.X + BulletPosition.X), (int)(Velocity.Y + BulletPosition.Y), Texture.Width, Texture.Height);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, BulletRectangle, Color.White);
}
}