• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

### #ActualFantasyVII

Posted 10 October 2012 - 03:42 PM

Hello everyone,

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.

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);
}
}


### #2FantasyVII

Posted 10 October 2012 - 03:41 PM

Hello everyone,

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.

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);
}
}


### #1FantasyVII

Posted 10 October 2012 - 03:40 PM

Hello everyone,

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.

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.

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 + Position.X), (int)(Velocity.Y + Position.Y), Texture.Width, Texture.Height);
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, BulletRectangle, Color.White);
}


PARTNERS