- Home /
Need some help with my gun reloading script
I've made a script for reloading my gun. It has a set number of bullets that are in the gun, a set number of bullets that can be in the "clip" at once, and a set number of bullets that are in reserve, like extra magazines. And all of these are public variables so I can put this one script on whatever guns I put in my game and simply set the parameters to whatever the specific gun requires. Here's the script:
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour
{
public int ammoCapacity;
public int ammo;
public int damage;
public int bulletsRemaining;
public GUIText AmmoCounter;
public bool canFire;
void Start ()
{
canFire = true;
}
void Update ()
{
if(ammo <= 0)
{
canFire = false;
if(bulletsRemaining >= ammo)
{
Reload();
bulletsRemaining -= ammo;
ammo = ammoCapacity;
}
}
if(canFire == true)
{
if(Input.GetButton("Fire1"))
{
animation.Play();
audio.Play();
ammo--;
Fire();
}
}
AmmoCounter.text = "Ammo:" + ammo + "/" + bulletsRemaining;
}
void Fire()
{
float range= Mathf.Infinity;
Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, range))
{
if(hit.transform.gameObject.tag == "Building")
{
hit.transform.GetComponent<BuildingHealth>().health -= damage;
}
}
}
IEnumerator Reload()
{
yield return new WaitForSeconds (5);
canFire = true;
}
}
The goal is to have the player shoot till the magazine is empty, wait a few seconds, in which time a reload animation will be played(not there yet, but in the future this will be the case) then subtract the number of bullets that can fit in the magazine from the bullets in reserve.
What happens now is the current clip is emptied, it refills instantly, but there are no bullets subtracted from the reserve, and the canFire variable is never re-set to true. I need to know why canFire remains false, how to set the reload time as a public variable, and how to get the clip size to be subtracted from the reserve bullets. Thanks for your time!
Answer by yashpal · Nov 26, 2014 at 04:53 AM
Hello @caleb_b ,
I didn't tested it but might works
I find two problem here
(1) use StartCoroutine("Reload") in place of Reload();
(2)
if(ammo <= 0)
{
if(bulletsRemaining >= ammo)
{
StartCoroutine("Reload");
//bulletsRemaining -= ammo; // Here is ammo is always 0(zero) because of if(ammo <= 0). so use
if(bulletsRemaining >= ammoCapacity)
{
bulletsRemaining -= ammoCapacity;
ammo = ammoCapacity;
}
else
{
ammo = bulletsRemaining;
bulletsRemaining = 0;
}
}
}
Hope it helps you.
Thanks @yashpal that fixed it. Works fine now. Id still like an aswer to my other question though. How can I set the reload time (the WaitForSeconds in Reload() ) as a public variable?
@caleb_b, Yes you can make reloadTime as public variable.
public float Gun1ReloadTime = 5f;
public float Gun2ReloadTime = 6.3f;
IEnumerator Reload(float TimeToReload)
{
yield return new WaitForSeconds (TimeToReload);
canFire = true;
}
And Use
StartCoroutine ("Reload", Gun1ReloadTime);
or
StartCoroutine ("Reload", Gun2ReloadTime);
And for animation length use animation.clip.length.
Your answer
Follow this Question
Related Questions
Problem with animations(noob question i guess) 1 Answer
Renderer on object disabled after level reload 1 Answer
Multiple Cars not working 1 Answer
Reload clips on Gun-script not applied properly 1 Answer
Distribute terrain in zones 3 Answers