Random generated direction

im working on a shotgun that is supposed to fire numerous pellets at once.

but the pellets are all in the same place, i use the simple translate script,

var speed : float = 5.0;
function Update () {
 transform.Translate(Vector3(0,0,speed) * Time.deltaTime);

but im not sure where and how to insert a random number generator for the X and Y values.

anyone know how, or have a link?

Sometimes it’s hard to know what to search. Just try Unity shotgun , here i tried unity shotgun spread

In order to randomly affect the x and y travel direction of your pellets, instead of having Vector3(0, 0, speed) * Time.deltaTime, try using Random.Range. So therefore:

Vector3(Random.Range(minx, maxx), Random.Range(miny, maxy), speed) * Time.deltaTime

where minx and many are your minimum x and y translation vectors, and maxx and maxy are your maximum x and y translation vectors. Random.Range will choose a value between those values and apply that to your Vector3, thus giving you random pellet spread. :slight_smile:

Hope that helps,