Shotgun Spread not Spreading

Hey guys,

First time poster here. Having a very hard time getting my weapon to spread correctly when dealing with more than 1 bullet. It works perfect when dealing with just 1 shot but the bullets don’t position themselves correctly when dealing with more than that. Here is the code:

using UnityEngine;
using System.Collections;

public class Weapon_Firer : Weapon
{

    public Rigidbody smallBullet;
    public Rigidbody smallWaveBullet;

    public Transform shotPos;
	public Transform gunPos;

    public float shotForce;
	public float fireRate;
	public float fireWait;

    public bool isExplosive;
    public bool isWave;
    public bool isLaser;
    public bool isTesla;

    public float numShots;
    public float shotSpread;

    void Update()
    {

		if (Time.timeScale == 1)
		{
			if (Input.GetButton ("Fire1"))
			{
				if(fireWait >= fireRate)
				{
                    if (isWave)
                    {
                        for (int i = 0; i < numShots; i++)
                        {
                            var pelletRot = transform.rotation;
                            pelletRot.x += Random.Range(-shotSpread, shotSpread);
                            pelletRot.y += Random.Range(-shotSpread, shotSpread);

                            Rigidbody shot = Instantiate(smallWaveBullet, shotPos.position, pelletRot) as Rigidbody;
                            shot.AddForce(shotPos.forward * shotForce);
                        }
                        
                    }

                    else
                    {
                        for (int i = 0; i < numShots; i++)
                        {
                            var pelletRot = transform.rotation;
                            pelletRot.x += Random.Range(-shotSpread, shotSpread);
                            pelletRot.y += Random.Range(-shotSpread, shotSpread);

                            Rigidbody shot = Instantiate(smallBullet, shotPos.position, pelletRot) as Rigidbody;
                            shot.AddForce(shot.transform.forward * shotForce);
                        }
                    }				

					fireWait = 0;
				}
				else
				{
					fireWait++;
				}
			}		
		}
	}
}

Your problem of them not positioning themselves correctly is a bit too vague to understand what they are doing. I don’t know what the value of shotSpread is but as you are using it in a Random.Range there is nothing stopping two pellets spawning on top of or inside each other. If your pellet count is high and the spread value low then it is pretty likely to happen every time you fire. Since they are rigidbodies this will obviously cause them to bounce off each other and mess up. That’s probably what you should be addressing. Using shotPos.forward for the spreading pellets would also stop them spreading and just make them travel in the direction of the gun barrel.

However because I was bored I also cleaned up your script for you by rolling the firing into a single function and assigning the projectile to it rather than writing it out identically twice. Untested but should work.

using UnityEngine;
using System.Collections;

public class Weapon_Firer : Weapon
{
    public Rigidbody smallBullet;
    public Rigidbody smallWaveBullet;

    public Transform shotPos;
    public Transform gunPos;

    public float shotForce;
    public float fireRate;
    public float fireWait;

    public bool isExplosive;
    public bool isWave;
    public bool isLaser;
    public bool isTesla;

    public float numShots;
    public float shotSpread;


    void FireShotgun(Rigidbody projectileType)
    {
        for (int i = 0; i < numShots; i++)
        {
            Quaternion pelletRot = transform.rotation;
            pelletRot.x += Random.Range(-shotSpread, shotSpread);
            pelletRot.y += Random.Range(-shotSpread, shotSpread);

            Rigidbody shot = Instantiate(projectileType, shotPos.position, pelletRot) as Rigidbody;
            shot.AddForce(shot.transform.forward * shotForce);
        }
    }

    void Update()
    {
        //Probably not a good idea to be checking the time scale every single frame
        //If you need to check if the game is paused you should probably use a bool and check it after Fire1

        if (Input.GetButton("Fire1"))
        {
            //If isPaused == false etc

            if (fireWait >= fireRate)
            {
                if (isWave)
                {
                    FireShotgun(smallWaveBullet);
                }
                else
                {
                    FireShotgun(smallBullet);
                }
                fireWait = 0;
            }
            else
            {
                fireWait++;
            }
        }
    }
}