# Creating a spread of bullets

I have gotten my code so that it fires two bullets, but currently they fire to the exact same position, but I have been trying many different ways (without success) to angle the second bullet slightly to one side at a fixed angle.

My code:

``````if (lookRight) {
//first bullet
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
coolDown = Time.time + attackRate;
float rot_z = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
bPrefab.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 45);

//second bullet
Vector3 dir2 = (Input.mousePosition - sp).normalized;
Rigidbody2D bPrefab1 = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
coolDown = Time.time + attackRate;
float rot_z1 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
bPrefab1.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45);
}
``````

As always, any help what so ever is greatly appreciated!

You are applying the force on both the bullets in the same direction. Try changing the direction of applying force to second bullet.

``````bPrefab1.rigidbody2D.AddForce ((dir2 + new Vector3(0.1f, 0f, 0f) )* 650);
``````

In above code the direction of applying is deviated by 0.1f in x axis. Just get the logic and you can apply deviation in any way you want.

To spread the bullets randomly you can probably do something like:

``````Vector3 newDir = (dir.x + Random.Range(0.0f, 0.1f), dir.y + Random.Range(0.0f, 0.1f), dir.z + Random.Range(0.0f, 0.1f)).normalized;
Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
``````

This will simply vary the values a little and normalize it again.

# Edit

If you actually want to spread the bullets by a fixed angle you can try something like:

``````Quaternion rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45);
Vector3 newDir = rotation * dir;