- Home /
How let my machinegun(like the deathmachine in cod black ops) to shoot a projectile one after the other??
Here's the code
var colpi : float;//colpi in totale var tempo: float;//tempo tra un colpo e l'altro var colpo_fuori : GameObject;// copertura del proiettile var projectile : Rigidbody;// proiettile che esce dalla canna var Spawn1: Transform;// var Spawn2: Transform; // var Spawn3: Transform; // var Spawn4: Transform; //Le 6 zone dalle quali spara la mitragliatrice var Spawn5: Transform; // var Spawn6: Transform;// var Shoot_Bullet_Speed : int; // velocit dei proiettili var BulletCount : int; //Ammount of bullets remaining in the clip var MagCapacity = 999; //The maximum capacity of bullets in the clip</p> <p>function FixedUpdate () { if (Input.GetButton ("Fire1") && BulletCount >= 1) //check to see if ready to fire { clone1 = Instantiate(projectile, Spawn1.position, Quaternion.identity); clone1.AddForce(transform.forward*Shoot_Bullet_Speed); </p> <pre><code> clone2 = Instantiate(projectile, Spawn2.position, Quaternion.identity); clone2.AddForce(transform.forward*Shoot_Bullet_Speed); clone3 = Instantiate(projectile, Spawn3.position, Quaternion.identity); clone3.AddForce(transform.forward*Shoot_Bullet_Speed); clone4 = Instantiate(projectile, Spawn4.position, Quaternion.identity); clone4.AddForce(transform.forward*Shoot_Bullet_Speed); clone5 = Instantiate(projectile, Spawn5.position, Quaternion.identity); clone5.AddForce(transform.forward*Shoot_Bullet_Speed); clone6 = Instantiate(projectile, Spawn6.position, Quaternion.identity); clone6.AddForce(transform.forward*Shoot_Bullet_Speed); } </code></pre> <p>} </p> <p>
I don't understand what you're trying to do. It looks like you're making 6 bullets all at once. If you want them to fire in succession you should check to see how much time has elapsed and release the bullet one by one after a fixed interval.
Answer by zmar0519 · Apr 28, 2011 at 07:56 PM
Try this:
var projectile : Rigidbody;
var shotPower : float;
var fireRate : float;
private var lastShot : float;
function Update ()
{
if(Input.GetButton("Fire1") && Time.time > lastShot)
{
lastShot = Time.time + fireRate;
Shoot();
}
}
function Shoot()
{
var go : Rigidbody;
go = Instantiate(projectile, transform.position, transform.rotation);
go.velocity = transform.forward*shotPower;
}
What's happening is each time you shoot, it sets the last shot to the current time plus the fire rate, so that when it checks if Time.time is greater than last shot, it is during that frame. Hope it helps!!! :)
LastShot should be lastshot. But otherwise this looks good.
Answer by Bumbaz 1 · Apr 28, 2011 at 08:50 PM
Or you could use the handy InvokeRepeating.
var fireRate : float = .3; var delay : float = 0;
function Update()
{
if(Input.GetMouseButtonDown(0)) {
InvokeRepeating("Shoot", delay, fireRate);
}
if(Input.GetMouseButtonUp(0)) {
CancelInvoke("Shoot");
}
}
function Shoot() {
//Your instantiate code here
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Limited Fire Rate? 1 Answer
Question about Gun Sounds 1 Answer
How to put a pause in a function? 3 Answers
firing a shell from a tank turret 1 Answer
How to fire a gun using raycasting and still add force 1 Answer