Hi there,
First of all thank you for your time. I was wondering if anyone could help me with my script. I am new to Unity and am creating my own gun system. I have made it so the guns can reload and use an animation however whenever one gun reloads all the others do as well. I want it so the guns have their own separate reloading system. I have set it up in my mind that it should work here however it does not. I will attached the code and if anyone could help me I would honestly be so appreciative.
Thank you,
Jamie.
using UnityEngine;
using System.Collections;
public class GunManager : MonoBehaviour
{
//AK47
public float damageAK = 10f;
public float rangeAK = 100f;
public float impactForceAK = 1f;
public float fireRateAK = 15f;
//Pistol
public float damageP = 10f;
public float rangeP = 100f;
public float impactForceP = 1f;
public float fireRateP = 15f;
//Sniper
public float damageS = 10f;
public float rangeS = 100f;
public float impactForceS = 1f;
public float fireRateS = 15f;
//Ammo System
public int MaxAmmoAK = 10;
public int MaxAmmoP = 10;
public int MaxAmmoS = 10;
private int currentAmmoAK;
private int currentAmmoP;
private int currentAmmoS;
public float reloadTimeAK = 1f;
public float reloadTimeP = 1f;
public float reloadTimeS = 1f;
public int ammoCapacityAK = 10;
public int ammoCapacityP = 10;
public int ammoCapacityS = 10;
//Reloading System
private bool isReloadingAK = false;
private bool isReloadingP = false;
private bool isReloadingS = false;
private bool isShootingAK = false;
private bool isShootingP = false;
private bool isShootingS = false;
private float nextTimeToFire = 0f;
//Animators
public Animator animatorAK;
public Animator animatorP;
public Animator animatorS;
//++
public Camera fpsCam;
public ParticleSystem muzzle;
public GameObject impactEffect;
void Start()
{
currentAmmoAK = MaxAmmoAK;
currentAmmoP = MaxAmmoP;
currentAmmoS = MaxAmmoS;
}
void OnEnable()
{
isShootingAK = false;
isShootingP = false;
isShootingS = false;
animatorAK.SetBool("Shooting", false);
animatorAK.SetBool("Shooting", false);
animatorAK.SetBool("Shooting", false);
isReloadingAK = false;
isReloadingP = false;
isReloadingS = false;
animatorAK.SetBool("Reloading", false);
animatorP.SetBool("Reloading", false);
animatorS.SetBool("Reloading", false);
}
void Update()
{
if (isReloadingAK)
return;
if (isReloadingP)
return;
if (isReloadingS)
return;
if (currentAmmoAK <= 0)
{
StartCoroutine(ReloadAK());
return;
}
if (currentAmmoP <= 0)
{
StartCoroutine(ReloadP());
return;
}
if (currentAmmoS <= 0)
{
StartCoroutine(ReloadS());
return;
}
if (Input.GetKeyDown(KeyCode.R))
{
Melee melee = GetComponent<Melee>();
if (melee == enabled)
{
return;
}
StartCoroutine(ReloadAK());
return;
}
if (ammoCapacityAK <= 0)
{
MaxAmmoAK = 0;
currentAmmoAK = 0;
reloadTimeAK = 0;
}
if (ammoCapacityP <= 0)
{
MaxAmmoP = 0;
currentAmmoP = 0;
reloadTimeP = 0;
}
if (ammoCapacityS <= 0)
{
MaxAmmoS = 0;
currentAmmoS = 0;
reloadTimeS = 0;
}
if (GameObject.FindGameObjectWithTag("AK47") == enabled)
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRateAK;
Shoot();
//StartCoroutine(ShootingAnim());
}
}
if (GameObject.FindGameObjectWithTag("Pistol") == enabled)
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRateP;
Shoot();
//StartCoroutine(ShootingAnim());
}
}
if (GameObject.FindGameObjectWithTag("Sniper") == enabled)
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRateS;
Shoot();
//StartCoroutine(ShootingAnim());
}
}
}
IEnumerator ReloadAK()
{
if(GameObject.FindGameObjectWithTag("AK47"))
if (currentAmmoAK != MaxAmmoAK)
{
isReloadingAK = true;
Debug.Log("Reloading AK47");
animatorAK.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTimeAK - 0.25f);
animatorAK.SetBool("Reloading", false);
yield return new WaitForSeconds(0.25f);
currentAmmoAK = MaxAmmoAK;
isReloadingAK = false;
}
}
IEnumerator ReloadP()
{
if (currentAmmoP != MaxAmmoP)
{
isReloadingP = true;
Debug.Log("Reloading Pistol");
animatorP.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTimeP - 0.25f);
animatorP.SetBool("Reloading", false);
yield return new WaitForSeconds(0.25f);
currentAmmoP = MaxAmmoP;
isReloadingP = false;
}
}
IEnumerator ReloadS()
{
if (currentAmmoS != MaxAmmoS)
{
isReloadingS = true;
Debug.Log("Reloading Sniper");
animatorS.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTimeS - 0.25f);
animatorS.SetBool("Reloading", false);
yield return new WaitForSeconds(0.25f);
currentAmmoS = MaxAmmoS;
isReloadingS = false;
}
}
//IEnumerator ShootingAnim()
//{
//
// animator.SetBool("Shooting", true);
//
// yield return new WaitForSeconds(0.25f);
//// animator.SetBool("Shooting", false);
//
// }
public void Shoot()
{
if(GameObject.FindGameObjectWithTag("AK47") == enabled)
{
isShootingAK = true;
ammoCapacityAK -= 1;
AudioSource sniperSound = GetComponent<AudioSource>();
sniperSound.Play();
muzzle.Play();
currentAmmoAK--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, rangeAK))
{
//Debug.Log(hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damageAK);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForceAK);
}
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
if (GameObject.FindGameObjectWithTag("Pistol") == enabled)
{
isShootingP = true;
ammoCapacityP -= 1;
AudioSource sniperSound = GetComponent<AudioSource>();
sniperSound.Play();
muzzle.Play();
currentAmmoP--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, rangeP))
{
//Debug.Log(hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damageP);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForceP);
}
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
if (GameObject.FindGameObjectWithTag("Sniper") == enabled)
{
isShootingS = true;
ammoCapacityS -= 1;
AudioSource sniperSound = GetComponent<AudioSource>();
sniperSound.Play();
muzzle.Play();
currentAmmoS--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, rangeS))
{
//Debug.Log(hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damageS);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForceS);
}
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
}