I’m trying to make a script to my GUN, with many features like sound for firing, reloading, animation for the same, fire rate and more, but i’m facing a problem that’s when i’m reloading if i tap the fire button it stop the reload animation and fire, but this is not right, you have to wait the reload complete to fire again. I implemented a canFire script but it work when my bullets end, when i shot all 8 bullets than my weapon stops, but the problem with reloading was not fixed, can someone help me? Here is my script(ps: everything is in C# and the script is for a android game.):
using UnityEngine;
using System.Collections;
public class PistolBehaviour : MonoBehaviour {
public PistolBulletBehaviour bullet;
public Transform barrel;
public GUITexture fireButton;
public GUITexture reloadButton;
public Animation gunAnimation;
public AnimationClip reloadAnimation;
public AnimationClip fireAnimation;
public ParticleSystem fireParticle;
public AudioClip fireSound;
public AudioClip reloadSound;
public float fireRate = 2;
private float currentTimeToFire = 0;
private bool canFire = true;
public int amountBullets = 8;
public int munition = 56;
private int initBullets;
// Use this for initialization
void Start () {
initBullets = amountBullets;
}
// Update is called once per frame
void Update () {
if(canFire == false){
currentTimeToFire += Time.deltaTime;
if(currentTimeToFire > fireRate){
currentTimeToFire = 0;
canFire = true;
}
}
if (Input.GetTouch(0).phase == TouchPhase.Began && canFire && amountBullets > 0) {
if (fireButton.HitTest(Input.GetTouch(0).position)) {
Instantiate(bullet, barrel.position, barrel.rotation);
gunAnimation.clip = fireAnimation;
gunAnimation.Play();
fireParticle.Emit(1);
canFire = false;
amountBullets--;
audio.clip = fireSound;
audio.Play ();
}
}
if (Input.GetTouch(0).phase == TouchPhase.Began && amountBullets < initBullets){
if (reloadButton.HitTest(Input.GetTouch(0).position)){
if(munition > 0)
munition--;
if(amountBullets <= initBullets){
int tempBullets = initBullets-amountBullets;
amountBullets += tempBullets;
munition -= tempBullets;
}
gunAnimation.clip = reloadAnimation;
gunAnimation.Play();
audio.clip = reloadSound;
audio.Play ();
}
}
if(gunAnimation.isPlaying == true){
canFire = false;
}
else{
canFire = true;
}
}
}