I have a Player that shoots bullets from a gun. Depending on the gun, there are different amounts of bullets. With my Shoot() method I want the instantiated bullets to have a fixed spread each time they shoot.
I tried that with a for loop:
void shoot()
{
//Set Spread
float spread = 10;
float currentSpread = 0;
//Instantiate Bullet
for (int i = 0; i < amount; i++)
{
//Get Spread
if (i == 0)
{
currentSpread = 0;
}
else if (i == 1)
{
currentSpread = spread;
}
else if (i == 2)
{
currentSpread = -spread;
}
else if (i == 3)
{
currentSpread = spread * 2;
}
else if (i == 4)
{
currentSpread = -spread * 2;
};
//Instantiate
Bullet = Instantiate(PreFab, Canvas.position, Quaternion.Euler(0, 0, Player.rotation.y + currentSpread));
//Set Parent
Bullet.transform.SetParent(Canvas, false);
//Set Name
Bullet.gameObject.name = "TeamBulletOne";
}
}
The “Canvas” is where the Bullets (2d) spawn, the rest of the world is 3d.
However, when I shoot it randomly either instantiates the bullets with a slightly off rotation or they spawn with about 2 degrees difference, not with the 10 degrees that are set in the Shoot() position.
I have no idea why and no matter how often I change or go through the method, I can’t find out why it isn’t working…
I don´t have anything else that affects the shooting mechanism, the colliders on my bullets are set to ignore the player and the other bullets and gravity is of as well.
Does anyone have an idea what to do?