Hi, I have this bit of code, thought it would be best to use a for loop for this one.
But how would I best do to spread the bullets but still have all go forward and spread by time?

``````            if (Input.GetButton("Fire1") && timeStamp <= Time.time)
{
for (int i = 0; i < amountBullets; i++)
{

GameObject shot = GameObject.Instantiate(shotgunShell, transform.position + (transform.forward * 1), transform.rotation) as GameObject;

}
}
}
``````

You have a few technical hurtles in order to make this work. The first is that you need all the projectiles in the spread to ignore each other. If you don’t, the projectiles will hit each other and bounce all over the place. You could disable their colliders and use Raycasting for the hits and collisions. You could use Physics.IgnoreCollision() to disable the collisions between the different bullets. If you do use Physics.IgnoreCollision(), it might be best to rewrite your code so that the bullets pool (are reused) rather than instantiating and destroying the bullets.

If you a Google search for “unity3d shotgun spread,” you will find a number of hits that include scripts. In looking through them, I was concerned about the pattern of the hits with some of them, and one answer was just wrong. So I’m offering this (untested) function as an alternate method of calculating a spread:

``````#pragma strict

function Spread(aim : Vector3, distance: float, variance : float) : Vector3 {
aim.Normalize();
var v3 : Vector3;
do {
v3 = Random.insideUnitSphere;
} while (v3 == aim || v3 == -aim);
v3 = Vector3.Cross(aim, v3);
v3 = v3 * Random.Range(0.0, variance);
return aim * distance + v3;
}
``````

You pass it the aim vector (transform.forward in your case), and it returns a new aim vector that has a maximum radius variance from the original aim position at the specified distance. In other words, at the specified distance, your spread will be no more that 2 * variance across.