Hello
I have a problem where the bullet keeps following the mouse instead of taking mouse angle and direction and keep going.
I have this code and someone fixed it for me but i there is a small problem. The guy who fixed it used Sprite.SetVector and the problem is there is no Vector function in sprite class.
can you guys please help.
My code
float Speed = 10;
float BulletX = Sprite[i].GetPosition().x;
float BulletY = Sprite[i].GetPosition().y;
float AngleX = MouseX - BulletX;
float AngleY = MouseY - BulletY;
float vectorLength = sqrt(AngleX*AngleX + AngleY*AngleY);
float DirectionX = AngleX / vectorLength;
float DirectionY = AngleY / vectorLength;
float VelocityX = DirectionX * Speed;
float VelocityY = DirectionY * Speed;
Sprite[i].Move (VelocityX, VelocityY);
Fixed code
// This code is called only when the bullet is fired
float bullet_x=Sprite[i].GetPosition().x;
float bullet_y=Sprite[i].GetPosition().y;
float angle_x=MouseX-bullet_x;
float angle_y=MouseY-bullet_y;
float vector_length=sqrt(angle_x*angle_x + angle_y*angle_y);
Sprite[i].SetVector(angle_x/vector_length, angle_y/vector_length); // Each bullet stores a direction vector
// This code is called every update
float velocity_x = Sprite[i].GetVector().x * Speed;
float velocity_y = Sprite[i].GetVector().y * Speed;
Sprite[i].Move(velocity_x, velocity_y);
Hoe can I do this ?
Sprite.SetVector(angle_x/vector_length, angle_y/vector_length); //
Sprite.GetVector().x