- Home /
having trouble with ammo reload script. destorys extra ammo
Hello Everyone, I have a basic script that allows me to reload a gun when it hits 0.
The only problem is that when I reload the gun while it has some rounds left inside, the current amount in the gun gets destroyed and a brand new 6 bullets are put it.
How can I make it that it only reloads up to 6 ammo without destroying the ones already in the gun?
I know there are other things in the script I need to fix as well, but this is the one error I cant seem to figure out. (the rest I'm sure I can figure out)
Any help would be greatly appreciated. Thank you!
here is the script
var bulletType: Rigidbody;
var bulletSpeed = 10;
private var handgunROF = 0.5;
var gunIsHeld : boolean = false;
var playerAmmoCount:ammoCount;
private var handgunFire :boolean = true;
function Update ()
{
FireHandgun();
ReloadHandgun();
}
function FireHandgun()
{
if (!gunIsHeld)
{
return;
}
if (playerAmmoCount.roundsInRevolver > 0)
{
if (Input.GetButtonDown("Fire1")&& (handgunFire))
{
handgunFire=false;
clone = Instantiate(bulletType, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0,0, bulletSpeed));
Destroy (clone.gameObject, 0.2);
audio.Play();
particleSystem.Play();
playerAmmoCount.roundsInRevolver -=1; //subtracts 1 round from clip
yield WaitForSeconds(handgunROF);
handgunFire=true;
}
}
}
function ReloadHandgun()
{
if (Input.GetKeyDown("r"))
{
yield WaitForSeconds(playerAmmoCount.reloadtime);
playerAmmoCount.roundsInRevolver +=6;
if (playerAmmoCount.roundsRevolverAmmo <6)
{
playerAmmoCount.roundsInRevolver = playerAmmoCount.roundsRevolverAmmo;
playerAmmoCount.roundsRevolverAmmo -= playerAmmoCount.roundsRevolverAmmo;
}
else
{
playerAmmoCount.roundsRevolverAmmo -=6;
}
}
}
Answer by GameVortex · Jan 08, 2014 at 08:23 AM
I can't see anything that destroys the bullets that are there, it adds 6 bullets to the the gun when you reload no matter how many bullets you have left in the gun. You need to add in only as many bullets that there is space for. So it is a bad practice to start with adding 6 bullets.
So we need to do a couple of things to get this right:
1: Calculate how many bullets we want to reload based on how many bullets are left in the gun.
2: Clamp that number down to the number of bullets we have left as spare ammo if it is less than the amount we want to reload.
3: Reload the ammo
Bonus 4: Also, no need to run the reload if we have no bullets left or have 6 bullets in the gun.
Example:
if(playerAmmoCount.roundsInRevolver < 6 && playerAmmoCount.roundsRevolverAmmo > 0) //Bonus Step 4
{
yield WaitForSeconds(playerAmmoCount.reloadtime);
float reloadAmount = 6 - playerAmmoCount.roundsInRevolver; //Step 1.
reloadAmount = Mathf.Clamp(1, playerAmmoCount.roundsRevolverAmmo); //Step 2.
//Step 3.
playerAmmoCount.roundsRevolverAmmo -= reloadAmount;
playerAmmoCount.roundsInRevolver += reloadAmount;
}
Because it is a coroutine, you would probably want to add a bool so you can't start loading while loading.
thank you for your help! I'm sorry for not replying earlier. I usually receive an email to let me know someone replied, but it hasn't done it. But I fixed it. It is very close to yours but thank you for re$$anonymous$$ding me to fix bool so you cannot load while loading. I didn't even think about that!
Thank you again!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
gui help for ammo display 1 Answer
I Need Help Whit Gun! 2 Answers
C# Reload Script 1 Answer