- Home /
weapon reload malfuction
i made a shotgun script witch is working really good until i noticed that for some reason after i reload instead of using the bullet from my clip it use the bullets from my total ammunitions for about a seconds and then it start using the one from my clip so i looked at where i could of went wrong but i cant find it so any idea on how to solve this?
relevant part of the script :
function Update(){
if(Input.GetButton("Fire1") && allowFire && Time.time > nextFire){
var hit : RaycastHit;
curClip --;
nextFire = Time.time + fireRate;
PlayerProperty.AgroLvl = gunAgro;
audio.PlayOneShot(gunSound);
spreading = true;
spread += spreadPerSecond;
}
if(Input.GetButtonDown("r") && ammunition > 0 && curClip < maxClip){
Reload();
allowFire = false;
spreading = false;
}
if(curClip <= 0 && ammunition > 0){
allowFire = false;
spreading = false;
Reload();
}
else if(curClip <=0 && ammunition <= 0){
allowFire = false;
spreading = false;
}
if (count >= pellet){
count = 0;
}
}
function Reload(){
PlayerProperty.AgroLvl = baseAgro;
yield WaitForSeconds(reload);
if( maxClip <= ammunition && curClip <= 0){
ammunition = ammunition - maxClip;
curClip = maxClip;
allowFire = true;
}
if(maxClip > ammunition && curClip <= 0){
curClip = ammunition;
ammunition = 0;
allowFire = true;
}
if( maxClip <= ammunition && curClip > 0){
ammunition = ammunition - (maxClip - curClip);
curClip = maxClip;
allowFire = true;
}
if( (maxClip - curClip) >= ammunition && curClip > 0){
curClip = curClip + ammunition;
ammunition = 0;
allowFire = true;
}
if( (maxClip - curClip ) < ammunition && curClip > 0){
ammunition = ammunition - (maxClip - curClip);
curClip = maxClip;
allowFire = true;
}
}
Answer by perchik · Mar 05, 2014 at 08:51 PM
Your logic on reload is very confusing...lots of cases that seem to overlap...why not do something like this:
function Reload(){
yield WaitForSeconds(reload);
var bulletsNeeded :int = maxClip - curClip; //#bullets needed to fill my clip
var bulletsGot: int =0;
if(bulletsNeeded < ammunition) //there's enough bullets to give what I need to reload
{
ammunition = ammunition - bulletsNeeded; //take those bullets from ammo
bulletsGot = bulletsNeeded; //keep up with how many you took
}
else{ //theres not enough (may be 0, who cares)
bulletsGot= ammunition; //take everything we have left
ammunition = 0; //set ammunition to empty
}
curClip = bulletsGot; //put however many bullets we got into the clip
if(bulletsGot==0)//if we didn't get anything, means we were out of ammuntion
allowFire=false;
else
allowFire =true;
}
Note this is untested code. This covers all the cases but the logic seems a lot clearer to me.
well even if it does not work you gave me a general idea on how to do it thank you alot
Answer by Dblfstr · Mar 05, 2014 at 08:10 PM
I thinkit is because you Reload() which has a wait for seconds. Then you set allowFire adn spreading to false. Try rversing them, like in the next if statement.
if(Input.GetButtonDown("r") && ammunition > 0 && curClip < maxClip){
allowFire = false;
spreading = false;
Reload();
}
the problem is not when he reload but after it does what i mean is that the reload timer work but as soon as the clip is full ins$$anonymous$$d of shooting its clip it shoot its ammunitions for about 1 sec
I see. I cannot tell where, in this code, that the ammunition variable is accessible other than in the reload function. Except that you allow firing while reloading. This could cause what you see. When actually the bullet IS co$$anonymous$$g from your clip, but the Reload function is filling it right back up, so you see the ammunition go down, and not the clip.
Your answer
Follow this Question
Related Questions
Deactivate children with .active = false 2 Answers
Controlling Weapons With MouseWheel 2 Answers
weapon sweep 1 Answer
Weapon Switching 2 Answers
How do I add ammo and reloads? 1 Answer