Hello all
First of all I apologize up front for posting such a long code.
I am truly sorry for it, but i have been struggling with for a while now.
My goal is to create a gun script, where I have a panel showing the numbers of bullet for current magazine.
There should maximum be 30 bullets in a magazine.
Player can maximum have 3 magazine clips.
This is the first script I have written by my self, no doubt there must be many easier ways, but I ended up hardcoding everything.
I have the panel and the UI text for the bullets. I have 3 images of clips.
What I cant seem to figure out, is:
- When bullets are empty from the first clip, how to switch over to a second clip?
- When pressing “R” (reload) it should fill up taking bullets from available clip
public float reloadTime = 1.5f;
private int maxAmmo = 30;
private int maxClips = 3;
//.....................
private int currentAmmo;
private int currentClip;
//......................
bool IsReLoading = false;
bool clipEmpty;
bool clipsFull;
bool allClipsEmpty;
bool clip1Full;
bool clip2Full;
bool clip3Full;
int clipsLeft;
public Image clipMag1;
public Image clipMag2;
public Image clipMag3;
public int clipAmmo1 = 0;
public int clipAmmo2 = 0;
public int clipAmmo3 = 0;
private void Start()
{
//DISBALED IN UPDATE......
clipMag1 = GetComponent<Image>();
clipMag2 = GetComponent<Image>();
clipMag3 = GetComponent<Image>();
allClipsEmpty = true;
}
// SCENARIO AFTER PICKING UP FIRST MAGAZINE
private void OnTriggerEnter2D(Collider col)
{
if(col.tag == "Player")
{
if( allClipsEmpty)
{
clipMag1.enabled = true;
clipAmmo1 = maxAmmo;
clip1Full = true;
clip2Full = false;
clip3Full = false;
}
if( clip1Full = true )
{
clipMag2.enabled = true;
clipAmmo2 = maxAmmo;
clip2Full = true;
clip3Full = false;
}
if( clip1Full == true && clip2Full = true )
{
clipMag3.enabled = true;
clipAmmo3 = maxAmmo;
clip3Full = true;
clipsFull = true;
}
else if(clipsFull)
{
Debug.Log("AMMO IS FULL");
}
}
}
void Update()
{
// Shoot
if(Input.GetButtonDown(Keycode("Fire1"))
{
//Shoot
}
if(allClipsEmpty == true)
{
clipMag1.enabled = false;
clipMag2.enabled = false;
clipMag3.enabled = false;
}
if(IsReLoading)
return;
if(Input.GetKeyDown(KeyCode.R))
{
if(!allClipsEmpty)
{
StartCourotine(ReLoading());
return;
}
}
else
{
Debug.Log("NO CLIPS");
}
}
}
IEnumerator ReLoading()
{
IsReLoading = true;
Debug.Log("RELOADING");
//FILL CLIP 3 AMMO
if(clipAmmo1 >= 30 && clipAmmo2 >=30 )
{
clipAmmo3 = maxAmmo;
clipMag1.enabled = true;
clipMag1.enabled = true;
clipMag1.enabled = true;
bool clip1Full = true;
bool clip2Full = true;
bool clip3Full = true;
}
//FILL CLIP 2 AMMO
if(clipAmmo3 <=0 && clipAmmo1 >=30 )
{
clipAmmo2 = maxAmmo;
clipMag1.enabled = true;
clipMag1.enabled = true;
clipMag3.enabled = false;
bool clip1Full = true;
bool clip2Full = true;
bool clip3Full = false;
}
//FILL CLIP 1 AMMO
if(clipAmmo3 <=0 && clipAmmo2 <=0 )
{
clipAmmo1 = maxAmmo;
clipMag1.enabled = true;
clipMag1.enabled = false;
clipMag3.enabled = false;
bool clip1Full = true;
bool clip2Full = false;
bool clip3Full = false;
}
yield return new WaitForSeconds(reloadTime);
// After above specified seconds = reloadTime then ammo will be filled update
IsReLoading = false;
}