# Question how to shoot bullets at a radial direction

Discussion in 'Scripting' started by ccgbagnet, Apr 3, 2024.

1. ### ccgbagnet

Joined:
Oct 3, 2013
Posts:
6
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.

Code (CSharp):
1. intprojectileCount = 5;
2. Quaternion gunRotation = GetGunGlobalRotation();
3.
4. for (var i = 0; i < projectileCount; i++)
5. {
6.      Projectile projectile = Instantiate(projectilePrefab);
7.      projectile.transform.position = gunBarrelPos;
8.      projectile.rigidbody.velocity = (gunRotation * Vector3.forward) * speed;
9. }

File size:
149.8 KB
Views:
16
2. ### Lekret

Joined:
Sep 10, 2020
Posts:
367
This can be done in different ways from math perspective, but I would do something like that:
Code (CSharp):
1. int projectileCount = 10;
2. float arcAngle = 90;
3. Quaternion fromRotationOffset = Quaternion.Euler(0, 0, -arcAngle / 2);
4. Quaternion toRotationOffset = Quaternion.Euler(0, 0, arcAngle / 2);
5. Quaternion fromRotation = gunRotation * fromRotationOffset;
6. Quaternion toRotation = gunRotation  * toRotationOffset;
7.
8. for (var i = 0; i < projectileCount; i++)
9. {
10.     float lerpT = i / (projectileCount - 1);
11.     Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
12.     // Do something with rotation
13. }

ccgbagnet, samana1407 and zulo3d like this.
3. ### zulo3d

Joined:
Feb 18, 2023
Posts:
1,054
A couple of corrections to Lekret's nice code:
Code (CSharp):
1.     int projectileCount = 10;
2.     float arcAngle = 90;
3.     Quaternion fromRotationOffset = Quaternion.Euler(0, -arcAngle / 2, 0);
4.     Quaternion toRotationOffset = Quaternion.Euler(0, arcAngle / 2, 0);
5.     Quaternion fromRotation = gunRotation * fromRotationOffset;
6.     Quaternion toRotation = gunRotation  * toRotationOffset;
7.
8.     for (var i = 0; i < projectileCount; i++)
9.     {
10.         float lerpT = (float)i / (projectileCount - 1);
11.         Quaternion projectileRotation = Quaternion.Lerp(fromRotation, toRotation, lerpT);
12.         // Do something with rotation
13.     }

Last edited: Apr 3, 2024
Lekret and ccgbagnet like this.
4. ### ccgbagnet

Joined:
Oct 3, 2013
Posts:
6
This works perfectly. @zulo3d is also correct that it should be modifying the Y part. Thank you so much!

Lekret likes this.