- Home /
Rayshoot Reload Problem (Can still shoot)
With my rayshoot script, when I manually reload (press R), I can constantly keep pressing r and reloading. By this I mean:
I have 25 bullets out of 32 bullets. I can press R to reload, now what happens is I can keep pressing R and then the sound will play everytime I press it. I can also keep shooting. Pressing R does not extend the reload time.
I want this to stop, but the problem is is that I do not know how. Please help, thank you in advance.
THE FULL SCRIPT IS NOT POSTED HERE
Here are some parts to the script:
Reload Sound
void PlayReloadAudio(){
audio.PlayOneShot( ReloadSound);
}
What I use to indicate a reload, and the use of reloading
BulletsLeft--;
if( BulletsLeft < 0){
BulletsLeft = 0;
}
if(BulletsLeft == 0){
Reload();
}
}
//and this
if(BulletsLeft < BulletsPreClip && Input.GetKeyDown(KeyCode.R)) {
Reload();
}
Reloading Part
void Reload (){
StartCoroutine( ReloadSpeed());
}
IEnumerator ReloadSpeed(){
PlayReloadAudio();
muzzleFlash.emit = false;
Light1.active = false;
Light2.active = false;
Light3.active = false;
yield return new WaitForSeconds(ReloadTime);
if(Clip > 0){
BulletsLeft = BulletsPreClip;
}
}
add a boolean for example var canShoot : boolean = true;
, and set it to false while reloading, then true when loaded. Then use this boolean when you fire e.g.
if (Input.Get$$anonymous$$ouseButton(0) && canShoot) {..shoot!;}
Answer by SheepHugger · Jul 25, 2012 at 12:33 PM
Yes.
If the function for firing is something like:
function Fire()
{
if (!isReloading)
{
//Do stuff, fire the gun
}
}
Alternatively, the function call can be like this:
if (Input.GetButton("Fire1") && !isReloading)
{
Fire();
}
In the latter, every time you press Fire1 button, it checks if the gun is not reloading. '!' in front is the same as
if(isReloading == false)
i tried the boolean and it worked thanks! it was so simple...on behalf of my part im sorry i couldnt see something so simple! thanks again
Answer by SheepHugger · Jul 23, 2012 at 07:13 PM
Or alternatively, the boolean can be something like:
isReloading : boolean = false;
Also, set the timer for reloading:
reloadTimer : float = 0;
And set how long it takes to reload:
// It takes now 3.5 seconds to reload
reloadTime : float = 3.5;
You can also have a variable for total amount of bullets that the player has, so he'll eventually run out:
BulletsTotalLeft : int = 3 * BulletsPreClip;
then, if you run out of bullets in your clip or hit R and you still have bullets to reload into the gun but aren't currently reloading:
// If your clip runs out of bullets
if (BulletsLeft <= 0 && BulletsTotalLeft > 0 && !isReloading){
Reload();
}
// If you hit 'R' to reload
if (Input.GetKeyDown(KeyCode.R) && BulletsTotalLeft > 0 && !isReloading){
Reload();
}
And, the Reload() function will do something like function Reload(){ isReloading = true; reloadTimer = reloadTime; PlayReloadAudio(); }
Then, place this on Update(), to check if you're reloading and to countdown the time:
function Update(){
if (isReloading){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
// Reloading ends now
isReloading = false;
// Check if BulletsTotalLeft is less than BulletsPerClip
if (BulletsTotalLeft < BulletsPreClip){
// if there's less bullets total left than per clip, reload only that many
BulletsLeft = BulletsTotalLeft;
}
else{
// Otherwise just fill the clip
BulletsLeft = BulletsPreClip
}
}
}
Hope that helps you with what you're doing.
SheepHugger out.
I like the fast response, and the very descriptive path that you took to develop your answer. I already have all of what you stated (bullets per clip, clips, etc.) I'm just focusing on disabling the Shoot function while reloading. The boolean way may work, I haven't tried it yet. But is there a way to disable a function?
Your answer
Follow this Question
Related Questions
Gun reload and clip size 0 Answers
FPS Shoooting Problem 1 Answer
When I kill one enemy, they all die? 0 Answers
Reload FPS Animation Not Playing (Javascript) 1 Answer
Gun Shooting Animation Won't Play 0 Answers