- Home /
ShotGun Spread Unity,How to Add bullet spread to a shotGun
I am trying to make an fps game and I have an inventory system implemented. However when I add the shotgun and then shoot it the bullets do spread but they spread on the horizontal axis and it looks kinda odd. Can some one walk me through how to implement proper shooting with bullet spread. Here is what I have so far. another issue with that is that when the player is looking left the bullets would not spread but instead they would go in line with each other.
public int BulletsShot; // Total bullets show per Shot of the gun
public float BulletsSpread; // Degrees (0-360) to spread the Bullets
public GameObject BulletTemplate; // Bullet to fire
public void Fire()
{
float TotalSpread = BulletsSpread / BulletsShot;
for (int i = 0; i < BulletsShot; i++)
{
// Calculate angle of this bullet
float spreadA = TotalSpread * (i + 1);
float spreadB = BulletsSpread / 2.0f;
float spread = spreadB - spreadA + TotalSpread / 2;
float angle = cam.transform.eulerAngles.y;
// Create rotation of bullet
Quaternion rotation = Quaternion.Euler(new Vector3(0, spread + angle, 0));
// Create bullet
GameObject bullet = Instantiate(BulletTemplate, tip.position, tip.rotation);
bullet.GetComponent<Rigidbody>().AddForce(transform.forward * pelletFireVel);
}
}
Thanks in advance.
public class shoot : $$anonymous$$onoBehaviour
{
public int BulletsShot; // Total bullets show per Shot of the gun
public GameObject BulletTemplate; // Bullet to fire
public Transform shootTransform;
public float maxSpread = 90f;
public void Fire()
{
for (int i = 0; i < BulletsShot; i++)
{
var randomX = Random.Range(-maxSpread, maxSpread);
var randomY = Random.Range(-maxSpread, maxSpread);
var randomZ = Random.Range(-maxSpread, maxSpread);
// Create bullet
GameObject bullet = Instantiate(BulletTemplate, shootTransform.position, shootTransform.rotation);
bullet.transform.Rotate(randomX, randomY, randomZ);
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 500);
}
}
// Start is called before the first frame update
void Start()
{
Fire();
}
}
Answer by n_rusev · Apr 04, 2020 at 09:54 AM
Hello. I think you are overcomplicating the problem. When you consider a real life shotgun the bullets will not spread in billboard fashion (although it might look like that) as you are trying to do, but in a 3d volume instead. Probably just adding some randomness in all directions to your AddForce vector (line 21) will do it just fine. Maybe something like this:
//Declare public float to edit in unity editor
public float maxSpread;
//Add randomness to every bullet direction
Vector3 dir = transform.forward + new Vector3(Random.Range(-maxSpread,maxSpread), Random.Range(-maxSpread,maxSpread), Random.Range(-maxSpread,maxSpread));
bullet.GetComponent<Rigidbody>().AddForce(dir * pelletFireVel);
Because in real life the buckshot inside the shell will interact with each other and some will slow down, some will shift up, down, left and right, and that is what adding randomness to add directions will do.
Also, name your variables starting with lower case letter - much convenient to read and no problems with var names being the same as type names.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make my gun semi-auto? 1 Answer
Unity 2D shogun,Unity 2D shogun spread? 0 Answers
Shotgun Spread Issue 1 Answer
How to make shotgun spread in Unity 2D (without Raycasting) 2 Answers