Moving sprite from one vector to the other
Posted
by
user2002495
on Game Development
See other posts from Game Development
or by user2002495
Published on 2013-07-12T09:04:37Z
Indexed on
2013/11/03
4:14 UTC
Read the original article
Hit count: 150
I'm developing a game where enemy can shoot bullets towards the player. I'm using 2 vector that is normalized later to determine where the bullets will go.
Here is the code where enemy shoots:
private void UpdateCommonBullet(GameTime gt)
{
foreach (CommonEnemyBullet ceb in bulletList)
{
ceb.pos += ceb.direction * 1.5f * (float)gt.ElapsedGameTime.TotalSeconds;
if (ceb.pos.Y >= 600) ceb.hasFired = false;
}
for (int i = 0; i < bulletList.Count; i++)
{
if (!bulletList[i].hasFired)
{
bulletList.RemoveAt(i);
i--;
}
}
}
And here is where i get the direction (in the constructor of the bullet):
direction = Global.currentPos - this.pos;
direction.Normalize();
Global.currentPos
is a Vector2 where currently player is located, and is updated eveytime the player moves.
This all works fine except that the bullet won't go to player's location. Instead, it tends goes to the "far right" of the player's position.
I think it might be the problem where the bullet (this.pos
in the direction) is created (at the position of the enemy). But I found no solution of it, please help me.
© Game Development or respective owner