- Home /
Shotgun bullet spread
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;
shot.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}
Answer by robertbu · Jul 14, 2013 at 06:03 PM
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.
One solution I've seen for shotguns is to create the decals at the hit points using a Raycast, and not shoot any projectiles at all.
Nice one! Thanks!
EDIT
Actually could you explain the code a bit with comments or something so I can learn how it works? Even though it works I want to know how it works :D
I wanted to create a vector perpendicular to the aim vector. So I generate an arbitrary vector that that is not the aim vector (lines 6 - 8). The cross product between the aim vector and the arbitrary vector creates the perpendicular vector (line 9). Then the perpendicular vector is sized to the variance (line 10). Finally I create an aim vector with a magnitude of 'distance' and then offset it by the variance.
Note this does not create a uniform distribution across the possible disc. Ins$$anonymous$$d it it will have a higher percentage of the shots towards the center. But that is how I visualize a shotgun pattern, and the pattern should be consistent no matter where the gun is aimed.
$$anonymous$$ost of the answers I reviewed just made a random modification to the x, y, and z rotation of the aim vector. I was concerned these solutions would produce patterns that differed depending on the direction of the aim.
I am really happy with the bullets as they are, I thought of raycasts aswell, but will only take that road if needed. At the moment I added trails to the bullets and it looks awesome! $$anonymous$$ight test to make some kind of plain object always facing the camera or something later on. At the moment I dont have any path with the game, I just play around and have fun. Unity is like a sandbox game itself (T$$anonymous$$) :D
Thanks! really appreciate your help and comments mate!
Your answer
Follow this Question
Related Questions
Shooting at mouse position 1 Answer
bullets don't go forward 1 Answer
Issues with bullet not subtracting properly 1 Answer
2D Bullet texture follow ray and Bullet Holes 1 Answer
How to make bullets spread in unity2D? 0 Answers