I have a player's direction (playerDirection
).
I want to fire two bullets, one rotated by -30f, and one by 30f, so it looks like this:
Blue line is the playerDirection
. I'm looking for the green and the red.
I have calculated the red vector with:
bullet1Direction = Quaternion.Euler(0, 30f, 0) * playerDirection;
But this method doesn't seem to work for all cases because it produces negative angles etc.
So I've tried taking that red vector I've calculated and reflecting it with Vector3.Reflect
but I think it's for different purposes.
How do I do that?
edit just for reference
Turns out it works out of the box as DMGregory says. I had some issues in the algorithm.
Here's how the final version looks like, supporting shooting multiple bullets. I know that the algorithm probably could be simplified somehow.
for (int i = 0; i < bullets; i++)
{
// we will calculate the angle for each bullet
float angle = 0f;
if (evenCount) // if the bullet count is even or not
{
if (i % 2 == 0) // bullet 0, 2, 4
{
angle = playerDirection.y + ((i + 2) * 15f); // 30f, 60f, 90f...
}
else // bullet 1, 3, 5
{
angle = playerDirection.y - ((i + 1) * 15f); // -30f, -60f, -90f...
}
}
else
{
if (i == 0) angle = playerDirection.y; // bullet count is odd. we spawn one bullet at the player's direction
else
{
if (i % 2 == 1) // bullet 1, 3, 5
{
angle = playerDirection.y + ((i + 1) * 15f); // 30f, 60f, 90f...
}
else // bullet 2, 4, 6
{
angle = playerDirection.y - ((i) * 15f); // -30f, -60f, -90f...
}
}
}
newDirection = Quaternion.Euler(0, angle, 0) * playerDirection;
}
And here's a visual result! (6 bullets and 3 bullets)