Can't make shotgun bullets spread

Trying to make a shotgun fire multiple bullets with each bullet having a different angle from the player to create a spread. I have a working way to do accuracy with different guns, but when I apply it to the shotgun, it fires four bullets, but all the bullets have the same angle. Any ideas how to give each bullet a different angle?

Code:
The firing code (In the player script):

void Update() {
        /* SHOOTING */
        // If we have a gun
        if (currentGunType != "None") {
            // If we have pressed the shoot button
            if (Input.GetKeyDown (shootButton)) {
                // We are shooting
                isShooting = true;
            }
            // If we are shooting
            if (isShooting) {
                // If the player is using a shotgun
                if (currentGunType == "Shotgun") {
                    // Fire multiple shots
                    for (int i = 0; i < shotgunBulletAmount; i++) {
                        // Make a bullet at the position of the player in the direction of the mouse
                        Instantiate (bullet, Player.playerPos, bulletAngle);
                    }
               }
          }
     }
}

The bullet code (In the bullet script):

void Start() {
        /* ACCURACY */
        Player.bulletRot = Player.mouseRotation;
        Player.bulletRot += Bullet.bulletAccuracy;
        Player.bulletAngle = Quaternion.Euler (new Vector3(0, 0, Player.bulletRot - 90));
    }

I’m thinking it’s because the bullet rotations are being calculated on the same frame? Don’t know for sure though. Any help appreciated.

You are creating all bullets with same angle. Start runs only once and update every frame.

Also, consider to use enum types rather than strings at currentGunType

But won’t a different angle get calculated when each bullet is created? It works with every other weapon type, just not with the shotgun when I instantiate more than one bullet.

I was originally going to use an enum, but wanted to get something done quickly, so used a string instead. I will learn how to use enums at some point in the future.

for (int i = 0; i < shotgunBulletAmount; i++)
{
    // Make a bullet at the position of the player in the direction of the mouse
    Instantiate (bullet, Player.playerPos, bulletAngle);
}

Here you are instantiating the bullet without change the bulletAngle variable every loop iteration

for (int i = 0; i < shotgunBulletAmount; i++) {
                        /* ACCURACY */
                        Player.bulletRot = Player.mouseRotation;
                        Player.bulletRot += Bullet.bulletAccuracy;
                        Player.bulletAngle = Quaternion.Euler (new Vector3(0, 0,                                   Player.bulletRot - 90));
                        // Make a bullet at the position of the player in the direction of                         the mouse
                        Instantiate (bullet, Player.playerPos, bulletAngle);
                    }

I changed it to this. Not sure if this is what you were thinking I should try/do, but it gives the same result as before. Four bullets with the same angle. Sorry if you’re trying to help without giving me the full answer and I’m missing something stupidly obvious, it happens.

1 Like

you are creating all the bullets in the same frame, so for each bullet all the values are the same; therefore the bullets will all calculate the exact same thing.

If you want some spread to the bullets you are going to have to add some “randomness” to the calculation. Lookup the Random.Range(…) function on the api and do something like:

float spread;
...
Player.bulletAngle=Quaternion.Euler(newVector3(0, 0, Player.bulletRot + (Random.Range(-1.0f, 1.0f) * spread)));
/* ACCURACY */
Player.bulletRot = Player.mouseRotation;
Player.bulletAngle = Quaternion.Euler (new Vector3(0, 0, (Player.bulletRot + Random.Range(-12f, 12f)) - 90));

Used this in the end. Works like a charm, thanks so much. Seems like my original suspicious was correct with the different angles getting calculated on the same frame so they’re the same value!