1
\$\begingroup\$

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:

enter image description here

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)

enter image description here enter image description here

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Both solutions you mention — rotating with Quaternion.Euler in each direction, or using Vector3.Reflect — can work perfectly. If you're observing a result different than what you want, please include the full code you're using to compute both left and right vectors, and describe in detail in which cases the result differs from what you need, and how. \$\endgroup\$
    – DMGregory
    Commented Sep 20, 2019 at 21:03
  • \$\begingroup\$ Thanks DMGregory. Turns out there was an issue with my code. I was trying to normalize and clamp the angle, I don't even know why. Probably from some past bad-experience :) But I have also added the code to the question. \$\endgroup\$
    – Jacob
    Commented Sep 20, 2019 at 21:49
  • \$\begingroup\$ If you've solved your problem, please post your solution as an Answer below. Or, if you still need help, please edit your question to focus on the specific issue you need help with now. \$\endgroup\$
    – DMGregory
    Commented Sep 20, 2019 at 22:04
  • \$\begingroup\$ nice looking game :) \$\endgroup\$
    – Millard
    Commented Sep 20, 2019 at 22:17
  • \$\begingroup\$ docs.unity3d.com/ScriptReference/Vector3.Reflect.html \$\endgroup\$
    – virtouso
    Commented Sep 20, 2019 at 22:22

1 Answer 1

1
\$\begingroup\$

Turns out you can rotate the direction using this formula without problems:

newDirection = Quaternion.Euler(0, desiredAngle, 0) * playerDirection;

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .