ItsLurq
December 19, 2014, 3:22am
1
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;
bPrefab.rigidbody2D.AddForce (dir * 650);
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;
bPrefab1.rigidbody2D.AddForce (dir2 * 650);
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.

LRG
December 19, 2014, 1:20pm
3
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;
bPrefab.rigidbody2D.AddForce (dir * 650);
```

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;
bPrefab.rigidbody2D.AddForce (newDir * 650);
```

That should rotate the direction vector by your rotation as well.

Though, you might have a simpler alternative. You are (presumably) already rotating your sprite. You can simply add the force locally. Though unfortunately the 2D tools donâ€™t seem to have an AddRelativeForce method, you can:

bPrefab.rigidbody2D.AddForce(bPrefab.transform.forward * 650);

This should essentially just propel the bullet forward with respect to itself, which is probably what you want.