I’ve made a shotgun script which spawns pellet prefabs on a button click as follows:
var spreadFactor = 4;
var pelletSpeed = 50;
var pelletCount = 100;
fireRate = 1;
clipSize = 8;
if(Input.GetMouseButtonDown(0) && !firing && ammo != 0 && !isReloading && !fireCool){
firing = true;
fireCool = true;
var pellet : Rigidbody;
for(var i = 0; i<pelletCount; i++){
var pelletRot = transform.rotation;
pelletRot.x += Random.Range(-spreadFactor, spreadFactor);
pelletRot.y += Random.Range(-spreadFactor, spreadFactor);
pellet = Instantiate(bullet, transform.position, pelletRot);
pellet.velocity = transform.forward*pelletSpeed;
}
ammo -= 1;
AmmoText.text = "Ammo Left: " + ammo.ToString();
fireDelay(fireRate);
}
I know the code works because upon clicking the pellets do spawn.
A screenshot of my scene is below.
As you can see it is very simple and there are no stray objects around. However, when I fire in a specific direction/angle (specifically towards about 45º anticlockwise), the pellets refuse to instantiate in there and spawn outside of that area instead. It becomes impossible to shoot anything inside of that area.
Why does this happen?