Question by
JW_moore · Jul 27, 2016 at 06:06 PM ·
javascriptgunreload
Reloads correctly first time, but can't reload till count hits 0
In my game the gun will reload correctly the first time if its not 0 and will take the correct amount out of the reserves but after that first reload you can't reload again until the loaded ammo hits 0. What am i doing wrong?
var gunfire : AudioSource;
var gunded : AudioSource;
var gunreload : AudioSource;
function Start()
{
var aSources = GetComponents(AudioSource);
gunfire = aSources[0];
gunded = aSources[1];
gunreload = aSources[2];
}
function Update()
{
var ammo = 10;
var sum;
if (Input.GetButtonDown("Fire1"))
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
gunfire.Play();
GetComponent.<Animation>().Play("gunshot");
GlobalAmmo.LoadedAmmo -= 1;
}
else
{
gunded.Play();
}
}
if (Input.GetButtonDown("Reload"))
{
if (GlobalAmmo.CurrentAmmo > 1)
{
if (GlobalAmmo.LoadedAmmo < 10)
{
sum = ammo - GlobalAmmo.LoadedAmmo;
if (GlobalAmmo.CurrentAmmo >= 10)
{
GlobalAmmo.CurrentAmmo -= sum;
GlobalAmmo.LoadedAmmo += sum;
gunreload.Play();
}
else if (sum > GlobalAmmo.CurrentAmmo)
{
GlobalAmmo.LoadedAmmo += GlobalAmmo.CurrentAmmo;
GlobalAmmo.CurrentAmmo = 0;
gunreload.Play();
}
}
else if (GlobalAmmo.LoadedAmmo == 0)
{
if (GlobalAmmo.CurrentAmmo >= 10)
{
GlobalAmmo.LoadedAmmo += ammo;
GlobalAmmo.CurrentAmmo -= ammo;
gunreload.Play();
}
if (GlobalAmmo.CurrentAmmo < 10)
{
GlobalAmmo.LoadedAmmo = GlobalAmmo.CurrentAmmo;
GlobalAmmo.CurrentAmmo = 0;
gunreload.Play();
}
}
}
else
{
gunded.Play();
}
}
}
Comment
Best Answer
Answer by TBruce · Jul 27, 2016 at 06:35 PM
Change Update() to this
function Update()
{
var ammo = 10;
var sum;
if (Input.GetButtonDown("Fire1"))
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
gunfire.Play();
GetComponent.<Animation>().Play("gunshot");
GlobalAmmo.LoadedAmmo -= 1;
}
else
{
gunded.Play();
}
}
if (Input.GetButtonDown("Reload"))
{
if (GlobalAmmo.CurrentAmmo > 0)
{
if (GlobalAmmo.LoadedAmmo < 10)
{
sum = ammo - GlobalAmmo.LoadedAmmo;
if (sum >= GlobalAmmo.CurrentAmmo)
{
GlobalAmmo.LoadedAmmo += GlobalAmmo.CurrentAmmo;
GlobalAmmo.CurrentAmmo = 0;
gunreload.Play();
}
else
{
GlobalAmmo.CurrentAmmo -= sum;
GlobalAmmo.LoadedAmmo += sum;
gunreload.Play();
}
}
else if (GlobalAmmo.LoadedAmmo == 0)
{
if (GlobalAmmo.CurrentAmmo >= 10)
{
GlobalAmmo.LoadedAmmo += ammo;
GlobalAmmo.CurrentAmmo -= ammo;
gunreload.Play();
}
if (GlobalAmmo.CurrentAmmo < 10)
{
GlobalAmmo.LoadedAmmo = GlobalAmmo.CurrentAmmo;
GlobalAmmo.CurrentAmmo = 0;
gunreload.Play();
}
}
}
else
{
gunded.Play();
}
}
}
Your answer
Follow this Question
Related Questions
Muzzle flash script not working properly 0 Answers
Hitmarker still shows while reloading 0 Answers
How to add a Gun Reload feature to my script 0 Answers
fireRate javascript? 1 Answer