- Home /
how to make gun wait until done reloading?
I have a code written for my gun, and as soon as it reloads it begins shooting again, how do I make it wait until the sound for reloading the gun is done playing (it is 3 seconds).
I have my code attached and I would like to know what I need to add to make it wait until the audio finishes reloading.
#pragma strict
var reloadSound: AudioClip;
var fireSound: AudioClip;
var bullet : GameObject;
var bulletsPerSecond = 14.0;
private var shooting = false;
var bulletForce = 1000.0;
var bulletsInMag = 30;
var magsLeft = 10;
private var reloading = false;
var reloadCount = 0;
function Start() {
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
InvokeRepeating("Reload", 0.0, 1.0);
}
function Shoot() {
if (!shooting) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
audio.PlayOneShot(fireSound);
bulletsInMag --;
}
function Update(){
shooting = false;
Debug.Log(bulletsInMag);
if(Input.GetAxis("Fire1") && bulletsInMag > 0){
shooting = true;
}else if(Input.GetAxis("Fire1") && bulletsInMag <= 0){
shooting = false;
reloading = true;
}
}
function Reload(){
if (!reloading) return;
audio.PlayOneShot(reloadSound);
bulletsInMag = 30;
reloading = false;
}
I can cut out parts of the code if people need it cleaned up.
Answer by robertbu · Jul 24, 2013 at 02:14 AM
Why use InvokeRepeating for Reload()? Why not call it directly on line 33? Also, you can put a yield between lines 40 and 41 to pause the 3 seconds before reloading.
yield WaitForSeconds (3.0);
I tried that and It would just stop and not reload when I was out of bullets, rending the gun useless.
Here is a bit of restructuring:
#pragma strict
var reloadSound: AudioClip;
var fireSound: AudioClip;
var bullet : GameObject;
var bulletsPerSecond = 14.0;
private var shooting = false;
var bulletForce = 1000.0;
var bulletsIn$$anonymous$$ag = 30;
var magsLeft = 10;
private var reloading = false;
var reloadCount = 0;
function Start() {
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot() {
if (!shooting) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
audio.PlayOneShot(fireSound);
bulletsIn$$anonymous$$ag--;
}
function Update(){
shooting = false;
Debug.Log(bulletsIn$$anonymous$$ag);
if(Input.GetAxis("Fire1") && bulletsIn$$anonymous$$ag > 0 && !reloading){
shooting = true;
}else if(bulletsIn$$anonymous$$ag <= 0){
shooting = false;
Reload();
}
}
function Reload(){
reloading = true;
audio.PlayOneShot(reloadSound);
bulletsIn$$anonymous$$ag = 30;
yield WaitForSeconds(3.0);
reloading = false;
}
That did it, thanks, and why was waitforseconds stopping it when I had it the way I did above?
Without seeing the code exactly as you had it, I cannot be sure. One problem is that your reloading was contingent on 'Input.GetAxis("Fire1")'. Another problem was that your Reload() would be called by your Invoke() multiple times during the time the Reload() was waiting resulting in multiple coroutines. Neither issue should have stopped the gun from working at all.
It looked just like the above code you posted, but wait for seconds was before bullets in mag
Answer by chris_taylor · Jul 24, 2013 at 12:59 AM
if(Input.GetAxis("Fire1") && bulletsInMag > 0){
should also have !reloading
if(Input.GetAxis("Fire1") && bulletsInMag > 0 && !reloading){
but from what I can see the reloading looks instant, if you are playing a animation somewhere you should test if the animation is done before setting reloading to false.
*Edit: I would also only check for the input and if you need to reload when the weapon can fire so in this case in the shoot methods you are invoking
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
gun wont reload 1 Answer
having trouble with ammo reload script. destorys extra ammo 1 Answer
Reloading wont work 1 Answer
Yield And Return 2 Answers