- Home /
Shooting multiple bullets
I have my character shooting bullets in the game. But I want to know how my character can shoot multiple bullets at once.
Here is the example: https://www.youtube.com/watch?v=-H2ka0kH71s
Can someone show how to make my character or weapon shoot multiple bullets at once, per click?
My Shooting code:
var playerProjectile: GameObject;
function Update () {
if(Input.GetKeyDown("space") && Time.time > nextShot){
nextShot = Time.time * timeBetweenShots;
Instantiate(playerProjectile, _transform.position, Quaternion.identity);
}
}
For an accurate answer, please post your current shooting code.
Invoke or InvokeRepeating for simple timers in unity. please first search here for 100s of excellent long examples or unityGems.com
@Fattie - he is looking for how to create a 'fan' of bullet at each shot. I could not find a reference for him. I'll answer this when I have a bit more time if someone else does not get to it.
Answer by robertbu · May 25, 2013 at 11:20 PM
Here is some code that will shoot a fan shot:
#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.
Answer by FL · May 25, 2013 at 10:23 PM
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.