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 isao_hatake · Aug 31, 2013 at 03:49 AM · gunfirereloading

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;
     }    
 }

}

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
Best Answer

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).

Comment
Add comment · Show 2 · 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
avatar image isao_hatake · Aug 31, 2013 at 05:33 AM 0
Share

But i'm using it, check my five last words, i check if gunAnimation .isPlaying so canFire = false and else canFire = true.

avatar image DESTRUKTORR · Sep 01, 2013 at 12:58 PM 0
Share

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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Make player unable to shoot when reloading 3 Answers


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