- Home /
I have a problem with fire and reload in my GUN SCRIPT
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;
}
}
}
Answer by DESTRUKTORR · Aug 31, 2013 at 03:56 AM
use animation.IsPlaying() to check if you can fire or not. That way you can only fire at the end of the animation :P
[EDIT]
if(currentTimeToFire > fireRate){
currentTimeToFire = 0;
canFire = true;
}
You set it to true, here. That's why it's allowing you to shoot before the animation is over. You're using "canFire" too loosely. You should reduce your boolean checks and ensure that the more important ones are never overridden (such as the reload animation being interrupted because the fire-rate boolean checks as true, as happens, here).
But i'm using it, check my five last words, i check if gunAnimation .isPlaying so canFire = false and else canFire = true.
Didn't see that, my apologies. Edited the answer to what I believe the problem may be, now that I've seen that part.
Your answer
Follow this Question
Related Questions
Unity Coroutine problem 1 Answer
Script suddenly stopped working properly 1 Answer
How do shoot a bullet when the right button is clicked? 1 Answer
Weird problem with gunscript 2 Answers