Spreading bullet

Making bullets spread over a circle radius ?

I want to make a script that change the spreading of the bullets, on how fast you a walking. but I cant figure out how to interact with the Quaternion.

I have written a function for my game that calculates a random unit-vector inside a cone of a given opening angle around a given vector (does not have to be normalized). Put it into a static class (or remove the ‘this’ keyword, but that changes the way it is called).

Note: the distribution is not uniform, but approximately gaussian (I think…haven’t thought too much about it), so the resulting directions tend to be closer to the original direction than further away.

public static Vector3 RandomInCone(this Vector3 dir, float openingAngle) {
		Vector3 deviation = Random.insideUnitSphere;
		dir = dir.normalized;
		deviation -= Vector3.Dot(deviation, dir) * dir;

		return (dir * Mathf.Cos(openingAngle) + deviation * Mathf.Sin(openingAngle)).normalized;
	}

Use it like this:

newVector3 = SomeVector3.RandomInCone(angleInRadians);

You can then change the angle you pass to this function depending on the character’s speed.

Bingo, thanks. Was just what I have been missing.

Instantiate(bullet,transform.position,Quaternion.LookRotation(transform.forward.RandomInCone(Mathf.Deg2Rad * 10)));

works smooth