• 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
Question by Nourero · Jul 20, 2013 at 03:52 PM · gunreloadammo

Gun with ammo help

I need help with my gun script. I have been searching all over the internet but have not been able to find any help.

I have the gun working fairly good but when I try to make my bullets change then it does not work properly. My bullet count go down but when I press "r" for reloading then it does not fill up my bullets or lose a clip. if anyone could help I would be most grateful. If any other tips about my script I would also be very happy :)

 public Ray ray;
     public RaycastHit hit;
     public GameObject BulletMark;
     //public GameObject GunEffect;
     public float FireRate;
     public float GunRecoil = 10;
     int Bullets = 8;
     int AmmoLeft = 3;
     public GUIText Ammo;
     public GUIText AmmoClips;
     
     void Update () {
             if(Input.GetMouseButtonDown(0)&& Bullets>0){
                 //Start Shooting
                 InvokeRepeating("Shoot", 0.01f, 1/FireRate);
                 //GunEffect.active = true;
             
                  
 
         }
             if(Input.GetMouseButtonUp(0)){
                 //Stop Shooting
                 CancelInvoke("Shoot");
                 //GunEffect.active = false;
             
         }
     }
     
     
     void Shoot(){
         //Normal Raycast                    //Add a random value to mouse position(r
         ray = Camera.main.ScreenPointToRay(Input.mousePosition+Random.insideUnitSphere*GunRecoil);
         if(Physics.Raycast(ray, out hit, 1000)){
                 //Declear new GameObject variable
                 GameObject BulletHole;
                 //Create the shootmark prefab at the hit point
                 BulletHole = Instantiate (BulletMark, hit.point+(hit.normal*0.001f), transform.rotation) as GameObject;
                 //Rotate the created object at the face of the normal LookAt (hit.point+hit.normal)
                 BulletHole.transform.LookAt(hit.point+hit.normal);
                 BulletHole.transform.parent=hit.transform;
                 
                 hit.transform.gameObject.SendMessage("Hit", 5, SendMessageOptions.DontRequireReceiver);
         }
         
         Bullets = Bullets-1;
         Ammo.text = Bullets.ToString();
         if(Bullets==0){
             //CancelInvoke("Shoot");
         }else{
             if (Input.GetKeyDown("r")&& AmmoLeft>0){
                 Bullets = Bullets+8;
                 AmmoLeft = AmmoLeft-1;
                 InvokeRepeating("Shoot", 0.01f, 1/FireRate);
             }
         }
     }
     
     void OnTriggerEnter (Collider AmmoCrate){
         if(AmmoCrate.gameObject.name=="MoreBullets"){
             Destroy (AmmoCrate.gameObject);
             AmmoLeft = AmmoLeft + 2;
             AmmoClips.text = AmmoLeft.ToString();
         } 
     }
 }
Comment

People who like this

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

Answer by robertbu · Jul 20, 2013 at 04:57 PM

I'm not sure your desired behavior is here. You've put put your reload logic inside shoot, and you are using 'GetKeyDown()'. Combine the two and it means your reload logic will only work if the Shoot() is being called on exactly the same frame that the 'r' key goes down. These happening at the same time is unlikely. Also calling InvokeRepeating() here is strange. You could replace GetKeyDown() with GetKey(), but I think you want to move this logic into Update() so that the check for reload happens each frame regardless if the user is shooting or not.

Comment

People who like this

0 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 Nourero · Jul 20, 2013 at 11:45 PM 0
Share

Thank you very much. The script works great now :)

avatar image robertbu · Jul 21, 2013 at 04:21 AM 0
Share

If you question is answered, please click on the checkmark to close it out. Thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Reload After Magazine Is Empty 2 Answers

Gun - Ammo , reloading and UI problem. 1 Answer

Ammo Script 1 Answer

Problem with animations(noob question i guess) 1 Answer

how do i make a ammo and reload system 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