- Home /
Getting two barrels to fire alternately
I have a simple turret with two barrels, each able to instantiate and shoot a projectile. But I would like them to shoot alternately, like the old WW2 anti-air guns, rather than both shooting at once. Here is the script I have applied to both barrels:
public Rigidbody bullet;
public float power = 1500f;
public float ShootTimer = 0.01f;
public float nextFire = 0.0f;
//Shooter script
if(Input.GetButton("Fire1")&& Time.time > nextFire){
nextFire = Time.time + ShootTimer;
Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
Vector3 upwards = transform.TransformDirection(Vector3.up);
instance.AddForce(upwards * power);
}
}
}
Tweaking my Shoot Timer and Next Fire variables doesn't really give me satisfactory results. They may alternate for a few seconds but then for some reason it appears as if they both start to shoot faster. Also letting go of the fire button and pressing it again will cause them to start firing again at the same time. Am I missing some other variables?
Answer by YoungDeveloper · Aug 13, 2013 at 05:59 PM
Hi, create a bool and use true as left barrel shot, and false as right barrel shot. You should save each barrel separately so you could move them without touching the other one.
bool shootSequence;
void Start(){
shootSequence = false; //will start to shoot with right barrel, or set it as you wish
}
//if ready to shoot
if(shootSequence){ //is true
//shoot move/shoot left barrel
shootSequence !=shootSequence; //change bool
}
else{ //is false
//shoot move/shoot right barrel
shootSequence !=shootSequence; //change bool
}
I've tried to set the bool up to the best of my knowledge and shooting remains the same. Here is my script, which I'm not 100% sure is correct:
if(Input.GetButton("Fire1")&& Time.time > nextFire){
if(shootSequence){
nextFire = Time.time + ShootTimer;
Rigidbody instance = Instantiate(bullet, aim1.transform.position, aim1.transform.rotation) as Rigidbody;
Vector3 upwards = transform.TransformDirection(Vector3.up);
instance.AddForce(upwards * power);
shootSequence = !shootSequence;
}
else{
Rigidbody instance = Instantiate(bullet, aim2.transform.position, aim2.transform.rotation) as Rigidbody;
Vector3 upwards = transform.TransformDirection(Vector3.up);
instance.AddForce(upwards * power);
shootSequence = !shootSequence;
}
}
}
It shoots just the same as before.
It could be that you had a typo with the =! operator. Try "shootSequence =! shootSequence;" ins$$anonymous$$d of "shootSequence = !shootSequence;"
Yea, i made a mistake by typing != ins$$anonymous$$d of =!, but LeftyTwoGuns made it right. Try is simpler for the start, without all those timers, just one click one shot.
As it reads I think they would both fire at the same time. I think moving the time reset out of the inner if statement will solve that.
$$anonymous$$oving the ShootTimer out of the inner if statement did the trick, thanks! Fires just how I want it. And thanks for the bool suggestion, I was afraid I'd have to do some math or something.
Your answer
Follow this Question
Related Questions
Shooting Projectiles 1 Answer
machine gun js timing 1 Answer
Remeber the value between frame Update() 1 Answer
AI aim and shoot projectile at Player 1 Answer
I shoot a projectile, but its rotation is wrong (c#) 1 Answer