Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rodrigo Cordova · Oct 05, 2014 at 09:32 AM · c#variableboolean

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;
         }
 
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges