- Home /
Shooting while reload anim os playing
Hey guys, i have an annoying logic issue here, i've done my code, and did a variable called "canFire", so by the logic i'm not allow to shoot while my reload animation is playing. But i don't know whats wrong here, maybe a } or a { missing or one plus, can you guys help me please? thanks
using UnityEngine;
using System.Collections;
public class AKBehaviour : MonoBehaviour {
public GameObject bullet;
public Transform pipe;
public float fireRate = 2;
public float currentTimeToFire = 0;
private bool canFire = true;
public float animSpeed = 3;
//Animations Attached to the guns
public Animation weapon;
public AnimationClip shoot;
public AnimationClip idle;
public AnimationClip reload;
public AnimationClip zoomIn;
public AnimationClip zoomOut;
//BulletStuff
public int amountBullet = 16;
public int munition = 2;
private int initBullet;
//Audio
public AudioClip shotFX;
//Zoom Stuff
public float zoomAim = 30;
private float startZoom;
private bool inZoom = false;
// Use this for initialization
void Start () {
startZoom = Camera.main.fieldOfView;
initBullet = amountBullet;
weapon.animation["idle"].layer = 1;
weapon.animation["shoot"].layer = 2;
weapon.animation ["reload"].layer = 3;
}
// Update is called once per frame
void Update () {
if (canFire == false) {
currentTimeToFire += Time.deltaTime;
if (currentTimeToFire > fireRate) {
currentTimeToFire = 0;
canFire = true;
}
}
if (Input.GetButton ("Fire1") && canFire == true && amountBullet > 0) {
Atirar ();
}
if (Input.GetKeyDown (KeyCode.R)) {
Reload ();
}
if (Input.GetButton ("Fire2")) {
if (inZoom == false && animation.IsPlaying("zoomOut")==false) {
UseZoom ();
}
else if(inZoom ==true && animation.IsPlaying("zoomIn")==false){
CancelZoom();
}
}
}
private void Atirar(){
audio.clip = shotFX;
audio.Play ();
weapon.animation.Play("shoot");
weapon.animation ["shoot"].speed = animSpeed;
Instantiate (bullet, pipe.position, transform.rotation);
canFire = false;
amountBullet--;
}
private void Reload(){
if(munition>0){
CancelZoom();
munition--;
if(amountBullet<=initBullet){
amountBullet = 0;
amountBullet += initBullet;
}
weapon.animation.Play("reload");
}
if (weapon.animation.IsPlaying("reload")== true){
canFire = false;
}
else{
canFire = true;
}
}
private void UseZoom(){
if(inZoom ==false){
Camera.main.fieldOfView = zoomAim;
animation.CrossFade ("zoomIn");
inZoom = true;
}
}
private void CancelZoom(){
if (inZoom == true) {
Camera.main.fieldOfView = startZoom;
animation.CrossFade ("zoomOut");
inZoom = false;
}
}
}
Answer by N1warhead · Oct 05, 2014 at 09:55 AM
Try putting your canFire = false in the GetKey.R statement.
It has to deal with you bools, I sat here for 15 minutes looking at it, I was changing things up, but I figured I'd better not so you don't mess your script up more if that didn't work lol.
If that don't work, try learning IENumerators (Co-Routines)
http://answers.unity3d.com/questions/37411/how-can-i-wait-for-an-animation-to-complete.html
Your answer
Follow this Question
Related Questions
Change a Variable with another script not working (C#) 4 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Use one variable to reference another variable 1 Answer
Variable References in C# from JS 1 Answer