how to shoot bullets at a radial direction

Hi, I’m trying to create a half circle bullet spread (similar to the illustration). I’m not sure how to properly calculate. Right now what I have is a gun that can be aimed so I’m taking it’s rotation as a consideration as well and a bullet that shoots at the direction is pointing at.

Here is the pseudo-code of what I currently have. But now, I don’t know how to calculate angles and do a half circle bullet spread with respect to the current direction.

I’ve looked into tutorials like this but can’t understand enough to apply to what I currently have.

intprojectileCount = 5;
Quaternion gunRotation = GetGunGlobalRotation();

for (var i = 0; i < projectileCount; i++)
{
     Projectile projectile = Instantiate(projectilePrefab);
     projectile.transform.position = gunBarrelPos;
     projectile.rigidbody.velocity = (gunRotation * Vector3.forward) * speed;
}

This can be done in different ways from math perspective, but I would do something like that:

int projectileCount = 10;
float arcAngle = 90;
Quaternion fromRotationOffset = Quaternion.Euler(0, 0, -arcAngle / 2);
Quaternion toRotationOffset = Quaternion.Euler(0, 0, arcAngle / 2);
Quaternion fromRotation = gunRotation * fromRotationOffset;
Quaternion toRotation = gunRotation  * toRotationOffset;

for (var i = 0; i < projectileCount; i++)
{
    float lerpT = i / (projectileCount - 1);
    Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
    // Do something with rotation
}
3 Likes

A couple of corrections to Lekret’s nice code:

    int projectileCount = 10;
    float arcAngle = 90;
    Quaternion fromRotationOffset = Quaternion.Euler(0, -arcAngle / 2, 0);
    Quaternion toRotationOffset = Quaternion.Euler(0, arcAngle / 2, 0);
    Quaternion fromRotation = gunRotation * fromRotationOffset;
    Quaternion toRotation = gunRotation  * toRotationOffset;
  
    for (var i = 0; i < projectileCount; i++)
    {
        float lerpT = (float)i / (projectileCount - 1);
        Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
        // Do something with rotation
    }
2 Likes

This works perfectly. @zulo3d is also correct that it should be modifying the Y part. Thank you so much!

1 Like