#pragma strict
var playerProjectile: GameObject;
var numShots = 5; // Number of shots fired (should be odd);
var spreadAngle = 2.0; // Angle between shots
var timeBetweenShots = 0.5; // Minimum time between shots
private var nextShot = 0.0;
function Start() {
if (numShots / 2 * 2 == numShots) numShots++; // Need an odd number of shots
if (numShots < 3) numShots = 3; // At least 3 shots for a fan
}
function Update () {
if(Input.GetKeyDown(KeyCode.Space) && Time.time > nextShot){
nextShot = Time.time + timeBetweenShots;
var qAngle = Quaternion.AngleAxis(-numShots / 2.0 * spreadAngle, transform.up) * transform.rotation;
var qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
for (var i = 0; i < numShots; i++) {
var go : GameObject = Instantiate(playerProjectile, transform.position,qAngle);
go.rigidbody.AddForce(go.transform.forward * 1000.0);
qAngle = qDelta * qAngle;
}
}
}
There is one problem with this code. Since all the shots are Instantiated at the same location, they will collide with each other. You could save the game objects in an array and use Physics.IgnoreCollision() between all the pairs. An alternate solution is to start with the collider disabled and enabled it when the shots are free of each other. Here is a script:
#pragma strict
var radius = 0.051;
function Update () {
if (!collider.enabled && !Physics.CheckSphere(transform.position, radius)) {
collider.enabled = true;
}
}
‘radius’ is the radius of a sphere that could contain the projectile. The only issue with this solution is that the bullets will not start hitting anything until they are not touching. That means they will not hit anything at point-blank range.
Just make a prefab with multiples bullets. You probably need to ignore the collisions between the bullets, so use Physics.IgnoreLayerCollision(bulletLayerNumber,bulletLayerNumber,true) when the scene is loaded. Don’t forget to create a new layer for bullets.