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 Major · Jun 30, 2012 at 04:34 AM · functiondisableshootreload

Trouble With Disableing a Function

Okay so I have been working on an FPS, (like most people) and i need to disable my Reload function while I'm shooting my gun. I can get it to work by putting if(!Firing){return} at the beginning of the function and it won't reload at all. Even when I'm all out of ammo. Any help would be appreciated.

 var fireRate : float = .5;
 
 var bulletShell : Transform;
 
 var BulletShellSpead : float;
 
 var MuzzleFlash : Transform;
 
 var Smoke : Transform;
 
 var Sound : AudioClip;
 
 var strayFactor : float;
 
 var bulletPrefab : Transform;
 var bulletSpeed : int;
 
 var GrenadeSpeed : float;
 var Grenade : Transform;
 var GrenadeAmmo : int;
 
 var MaxAmmo : int;
 var CurrentClip : int = 10;
 var CurrentExtraAmmo : int = 100;
 var ClipSize : int = 10;
 
 var ReloadTime : float;
 var ClickSound : AudioClip;
 var ReloadSound : AudioClip;
 var Reloading : boolean = false;
 
 var Firing : boolean = false;
 
 private var nextFire = .5;
 
 function LateUpdate()
 {
     if(GrenadeAmmo < 0)
         GrenadeAmmo = 0;
         
     if(CurrentClip < 0)
     CurrentClip = 0;
     
     if(CurrentClip > MaxAmmo)
         CurrentClip = MaxAmmo;    
         
     if(Input.GetButtonDown("Fire1")&& CurrentClip <= 0)
         {
             NoAmmo();
         }
 }
 
 function Update() 
 {
         if(Input.GetKeyDown(KeyCode.R))
         {
             Reload();
         }
         
         if(Input.GetKeyDown(KeyCode.E))
             {
                 GrenadeThrow();
             }
         if(CurrentClip > 0)
         {
             if(Input.GetButtonDown("Fire1") && Time.time > nextFire)
             {
             nextFire = Time.time + fireRate;
             
             Firing = true;
             print("ouch");
             
             Shoot();
         
             var bulletShell = Instantiate(bulletShell, GameObject.Find("Spawn3").transform.position, Quaternion.identity);
             bulletShell.rigidbody.AddForce(transform.right * BulletShellSpead);
         
             var MuzzleFlash = Instantiate(MuzzleFlash, GameObject.Find("Spawn").transform.position, Quaternion.identity);
         
             var Smoke = Instantiate(Smoke, GameObject.Find("Spawn").transform.position, Quaternion.identity);
     
             animation.Play("PistolShoot");    
             audio.PlayOneShot(Sound);
             
             Firing = false;
             }    
         }
 }
 
 function GrenadeThrow()
 {
     if(GrenadeAmmo > 0)
         {
             var grenade = Instantiate(Grenade, transform.position, Quaternion.identity);
             grenade.rigidbody.AddForce(transform.forward * GrenadeSpeed);
         }
             GrenadeAmmo --;
 }
 
 function Shoot()
 {
     var randomNumberX = Random.Range(-strayFactor, strayFactor);
     var randomNumberY = Random.Range(-strayFactor, strayFactor);
     var randomNumberZ = Random.Range(-strayFactor, strayFactor);
         
     var bullet = Instantiate(bulletPrefab, GameObject.Find("Spawn").transform.position, transform.rotation);
     
     bullet.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
     bullet.rigidbody.AddForce(bullet.transform.forward * bulletSpeed);
             
     bullet.tag = "Bullet";
     CurrentClip --;
 }
 
 function Reload()
 {    
     if(!Firing)
     {
     return;
     }
 
     if(CurrentExtraAmmo <= 0)
     {
         NoAmmo();
     }
     else
     {
     if(CurrentClip <= 14)
     {
     Reloading = true;
     
     animation.Play("Reload");
     audio.PlayOneShot(ReloadSound);
     }
     yield WaitForSeconds(ReloadTime);
 
     Reloading = false;
 
     if(CurrentExtraAmmo >= ClipSize - CurrentClip)
     {
         CurrentExtraAmmo -= ClipSize - CurrentClip;
         CurrentClip = ClipSize;
     }
     
     if(CurrentExtraAmmo <= ClipSize - CurrentClip)
     {
         CurrentClip += CurrentExtraAmmo;
         CurrentExtraAmmo = 0;
         }
     
     }
     
     
 }
 function NoAmmo()
 {
     Firing = false;
     animation.Play("Emepty");
     audio.PlayOneShot(ClickSound);
 }
 
 function OnGUI()
 {
     if(CurrentClip <= 5)
     {
     GUI.contentColor = Color.red;
     }
 
     GUI.Box(Rect(Screen.width / 1.16, Screen.height / 80, 100, 20), ""+ CurrentClip + "/" + CurrentExtraAmmo);
 }
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 Berenger · Jun 30, 2012 at 04:41 AM

Firing = false; should be when Input.GetButtonUp("Fire1) is true, not Down.

By the way, not that it's important but, why do you use Update and LateUpdate ? I don't see a real reason here, besides being called after Update, but putting it at the end of Update would have the same effect.

Comment
Add comment · Show 5 · 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 Major · Jun 30, 2012 at 05:10 AM 0
Share

I didn't mean to put my question in the answers. If you see just ignore it. So would I add a GetButtonUp? Because that's what it sounds like.

And the answer to your question, it's easier to find in LateUpdate.

avatar image Berenger · Jun 30, 2012 at 02:17 PM 0
Share

I had something like that in $$anonymous$$d :

 if( GetButtonDown ){
     isFiring = true;
     // rest of the code
 }
 if( GetButtonUp )
     isFiring = false;
avatar image Major · Jun 30, 2012 at 05:40 PM 0
Share

Okay that makes sense, and I had done something like that, but removed it because I just needed to put the false statement to work better at the end of the firing sequence. And it didn't glitch and say that I was firing even though I wasn't. I tried to put that kind of thing in there, but it still won't allow me to reload.

avatar image delstrega · Jun 30, 2012 at 05:47 PM 0
Share

Shouldnt you return from Reload() WHEN you are firing? Why do you exit when you are NOT firing?

avatar image Major · Jun 30, 2012 at 06:33 PM 0
Share

Okay yeah but I still can reload while I'm firing! And now that I removed this !, I can reload now, but I can still reload while I'm firing.

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

6 People are following this question.

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

Related Questions

Function reload doesnt work 1 Answer

Reload/Shoot Animation 3 Answers

Java to C# conversion problem 1 Answer

BCE0005: Unknown identifier: 'Fire'. / Secondary shot? 1 Answer

C# - Ammo in Shoot Array?! 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