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 · Feb 21, 2012 at 08:40 AM · spawnbulletgunshoot

Troubles With A Shoot Script

Okay, so I am relativity new to scripting in Unity3d but not new to the program its self. I have a shoot script that I am having some difficulties with. If you apply the script to the first person controller (along with another script I will include), everything works as it is supposed to. Except for the fact that you can't reload until you run out of ammo(this includes that you can't reload when you have ammo left). When you run out of ammo you can reload just fine, even when you have ammo left. Once you run out of ammo the first time, you get a label that says "RELOAD". This label won't go away at all, not even once you have reloaded("r"). I found that those two things were a problem, and I have worked to my best efforts to try and figure them out. Some help would be greatly appreciated. If something didn't make sense, please tell me and I will try to explain it more clearly. Thanks!

Scripts: Firearm:

 var fireRate : float = 0.3;
 private var nextFire : float = 2.0;
 var Ammo : int = 30;
 var gunfire : AudioClip;
 var reload : AudioClip;
 var empty : boolean = false;
 var Projectile : Transform;
 
 function Update () 
 {
     empty = Ammo <= 0;
     if(Input.GetButton("Fire1")&&Time.time > nextFire)
         if(!empty)
             Fire();       
 
     if(Input.GetKeyUp("r"))
         if(empty)
             Reload();    
 }
 
 function Fire(){    
     audio.PlayOneShot(gunfire);
         nextFire = Time.time + fireRate;
         
         var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
         shot.rigidbody.AddForce (transform.forward * 3000);
         Ammo--;
 }
 
 function Reload(){        
     audio.PlayOneShot(reload);
     Ammo = 30;        
 }
 
 function OnGUI(){    
     GUI.Box(Rect(Screen.width - 100, 20, 100, 80), "");
     GUI.Label(Rect(Screen.width - 100, 35, 90, 20), "Ammo:" + Ammo);
     GUI.Label(Rect(Screen.width - 100, 20, 90, 20), "Health" + PlayerHealth.currentHealth);
     
     if(empty){
     GUI.contentColor = Color.red;
     GUI.Label(Rect(Screen.width - 100, 50, 90, 20), "RELOAD");            
         }
     }

PlayerHealth(Make sure the script is called this): var fireRate : float = 0.3; private var nextFire : float = 2.0; var Ammo : int = 30; var gunfire : AudioClip; var reload : AudioClip; var empty : boolean = false; var Projectile : Transform;

 function Update () 
 {
     empty = Ammo <= 0;
     if(Input.GetButton("Fire1")&&Time.time > nextFire)
         if(!empty)
             Fire();       
 
     if(Input.GetKeyUp("r"))
         if(empty)
             Reload();    
 }
 
 function Fire(){    
     audio.PlayOneShot(gunfire);
         nextFire = Time.time + fireRate;
         
         var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
         shot.rigidbody.AddForce (transform.forward * 3000);
         Ammo--;
 }
 
 function Reload(){        
     audio.PlayOneShot(reload);
     Ammo = 30;        
 }
 
 function OnGUI(){    
     GUI.Box(Rect(Screen.width - 100, 20, 100, 80), "");
     GUI.Label(Rect(Screen.width - 100, 35, 90, 20), "Ammo:" + Ammo);
     GUI.Label(Rect(Screen.width - 100, 20, 90, 20), "Health" + PlayerHealth.currentHealth);
     
     if(empty){
     GUI.contentColor = Color.red;
     GUI.Label(Rect(Screen.width - 100, 50, 90, 20), "RELOAD");            
         }
     }
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 · Feb 21, 2012 at 10:30 PM

empty, once set to true, is never set back to false on reload.

Comment
Add comment · Show 8 · 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 · Feb 23, 2012 at 03:08 AM 0
Share

Right so how do I make it set back to false once I reload?

avatar image Berenger · Feb 23, 2012 at 03:18 AM 0
Share

add empty = false; in Reload function.

avatar image Major · Feb 23, 2012 at 03:52 AM 0
Share

Okay I will thanks!

avatar image Major · Feb 24, 2012 at 04:43 AM 0
Share

I got an error. I put empty=false; at the end of the function that was in the brakets, is there another spot that i'm supposed to put it?

avatar image Berenger · Feb 24, 2012 at 05:06 AM 0
Share

I edited your script. The edit on this website takes a few hours for some reasons, you should see it tomorrow.

Show more comments

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

Shooting. Bullet floats and sprays 1 Answer

Bullet Drop With Raycast 4 Answers

Need a script for a gun that shoots bullet using raycast 2 Answers

Bullet Hole not inline with sight?(Center of Screen) 1 Answer

C# ShootController() 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