It could reload before, but now it can’t.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour {
private Animator anim;
private AudioSource _AudioSource;
public float range = 100f;
public int bulletsPerMag = 30;
public int bulletsLeft = 200;
public int currentBullets;
public enum ShootMode { Auto, Semi }
public ShootMode shootingMode;
public Transform shootPoint;
public GameObject hitParticles;
public GameObject bulletImpact;
public ParticleSystem muzzleFlash;
public AudioClip shootSound;
public float fireRate = 0.1f;
public float damage = 20f;
float fireTimer;
private bool isReloading;
private bool shootInput;
private Vector3 originalPosition;
public Vector3 aimPosition;
public float adsSpeed = 8f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
_AudioSource = GetComponent<AudioSource>();
currentBullets = bulletsPerMag;
originalPosition = transform.localPosition;
}
// Update is called once per frame
void Update () {
switch (shootingMode)
{
case ShootMode.Auto:
shootInput = Input.GetButton ("Fire1");
break;
case ShootMode.Semi:
shootInput = Input.GetButtonDown ("Fire1");
break;
}
if (shootInput)
{
if (currentBullets > 0)
Fire ();
else if(bulletsLeft > 0)
DoReload ();
}
if (Input.GetKeyDown (KeyCode.R)) {
if(currentBullets < bulletsPerMag && bulletsLeft > 0)
DoReload ();
}
if (fireTimer < fireRate)
fireTimer += Time.deltaTime;
AimDownSights ();
}
void FixedUpdate()
{
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo (0);
isReloading = info.IsName ("Reload");
//if (info.IsName ("Fire")) anim.SetBool ("Fire", false);
}
private void AimDownSights()
{
if (Input.GetButton ("Fire2") && !isReloading)
{
transform.localPosition = Vector3.Lerp (transform.localPosition, aimPosition, Time.deltaTime * adsSpeed);
} else
{
transform.localPosition = Vector3.Lerp (transform.localPosition, originalPosition, Time.deltaTime * adsSpeed);
}
}
private void Fire()
{
if (fireTimer < fireRate || currentBullets <= 0 || isReloading)
return;
RaycastHit hit;
if (Physics.Raycast (shootPoint.position, shootPoint.transform.forward, out hit, range))
{
Debug.Log (hit.transform.name + " found!");
GameObject hitParticlesEffect = Instantiate (hitParticles, hit.point, Quaternion.FromToRotation (Vector3.up, hit.normal));
hitParticlesEffect.transform.SetParent (hit.transform);
GameObject bulletHole = Instantiate (bulletImpact, hit.point, Quaternion.FromToRotation (Vector3.forward, hit.normal));
bulletHole.transform.SetParent (hit.transform);
Destroy (hitParticlesEffect, 1f);
Destroy (bulletHole, 2f);
if (hit.transform.GetComponent<HealthController> ())
{
hit.transform.GetComponent<HealthController> ().ApplyDamage (damage);
}
}
anim.CrossFadeInFixedTime("Fire", 0.1f);
muzzleFlash.Play();
PlaySoundShoot ();
currentBullets--;
fireTimer = 0.0f;
}
public void Reload()
{
if (bulletsLeft <= 0) return;
int bulletsToLoad = bulletsPerMag - currentBullets;
int bulletsToDeduct = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;
bulletsLeft -= bulletsToDeduct;
currentBullets += bulletsToDeduct;
}
private void DoReload()
{
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo (0);
if (isReloading) return;
anim.CrossFadeInFixedTime ("Reload", 0.01f);
}
private void PlaySoundShoot()
{
_AudioSource.PlayOneShot (shootSound);
}
}