This is a very confusion question to ask, but what I’m trying to make is a gun script that has a overall storage for a weapon and that when I run out of ammo or press R to reload, I want to take the correct amount from the storage and when it is not going to fill the full amount, it will put partial. I need help please. Script for reference
c#
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
public bool isRapidFire = false;
public int bulletInStore;
public float rapidFireSpeed = 1.5f;
public float defaultBulletSpeed;
public bool isSingleShot = false;
public bool hasAmmo = true;
public bool isAiming = false;
public float reloadSpeed;
private int ammo;
public int weaponAmmo;
public int damage = 1;
private float bulletSpeed;
public bool hasShot = false;
public Rigidbody bullet;
public Transform bulletSpawn;
public Transform aimingPosition;
public Transform nonAimingPosition;
public bool isReloading = false;
public bool isLightOff = false;
public GameObject gunLight;
public float lightBattery;
public float maxLight;
// Use this for initialization
void Start () {
ammo = weaponAmmo;
bulletSpeed = defaultBulletSpeed;
InvokeRepeating ("CountUp", reloadSpeed, reloadSpeed);
InvokeRepeating ("Reload", reloadSpeed, reloadSpeed);
InvokeRepeating ("BatteryLife", 2, 2);
}
// Update is called once per frame
void Update () {
if(lightBattery > maxLight){
lightBattery = maxLight;
}
if (Input.GetKeyDown (KeyCode.T) && isLightOff == false) {
gunLight.SetActive(false);
isLightOff = true;
} else if (Input.GetKeyDown (KeyCode.T) && isLightOff == true) {
gunLight.SetActive(true);
isLightOff = false;
}
Debug.Log (bulletSpeed);
Debug.Log (isRapidFire);
if (Input.GetKeyDown (KeyCode.R) && ammo > 0) {
ammo = weaponAmmo;
isReloading = true;
}
if (Input.GetKey (KeyCode.Y) && isRapidFire == false) {
bulletSpeed = rapidFireSpeed;
isRapidFire = true;
} else if (Input.GetKey (KeyCode.Y) && isRapidFire == true) {
bulletSpeed = defaultBulletSpeed;
isRapidFire = false;
}
if (Input.GetMouseButton (0) && hasShot == false && ammo > 0 && isSingleShot == true) {
Rigidbody clone;
clone = Instantiate (bullet, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
hasShot = true;
ammo -= 1;
} else if (Input.GetMouseButtonUp (0) && hasShot == true) {
Debug.Log ("Gun is done shooting for today...");
hasShot = false;
}
else if(Input.GetMouseButton (0) && ammo > 0 && isSingleShot == false){
Rigidbody clone;
clone = Instantiate (bullet, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
ammo -= 1;
}
/**if (Input.GetMouseButtonDown (1) && isAiming == false) {
transform.position = nonAimingPosition;
isAiming = true;
}
else if (Input.GetMouseButtonDown(1) && isAiming == true){
transform.position = aimingPosition;
isAiming = false;
}**/
}
void CountUp(){
if (ammo == 0) {
ammo = weaponAmmo;
}
}
void Reload(){
if (isReloading == true) {
ammo = weaponAmmo;
isReloading = false;
}
}
void OnGUI(){
if (ammo > 0) {
GUI.TextArea (new Rect (10, 10, 100, 30), ammo + "/" + weaponAmmo);
} else {
GUI.TextArea (new Rect (10, 10, 100, 30), "Reloading");
}
GUI.TextArea (new Rect (720, 10, 100, 30), "Battery: " + lightBattery);
}
void BatteryLife(){
if (isLightOff == false) {
lightBattery -= 0.5f;
}
if (lightBattery < 0 || isLightOff == true) {
lightBattery += 0.5f;
}
if (lightBattery == 0) {
isLightOff = true;
}
}
}