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 BawsAnimations · Dec 13, 2012 at 11:02 PM · c#gameshootreloadammo

C# - Ammo in Shoot Array?!

I need to implement ammo/reload function. How would i do this in my script?

 using UnityEngine;
 
 public class Shooter : MonoBehaviour
 
 
 
 {
     void Update ( )
     {
         // 1. Wait for a mouse click.
         if ( Input.GetButtonDown( "Fire1" ) )
         {
             Shoot( );
             
             
         }
     }
 
     void Shoot ( )
     {
         // 2. Create a ray that travels from your camera 
         //    in the direction it's facing.
         Ray ray = new Ray( transform.position, transform.forward );
         RaycastHit hit;
         if ( Physics.Raycast( ray, out hit ) )
         {
             // 3. Check the first thing it hits, is it your enemy?
             //    If so (actually there is nothing defined as enemy);
             hit.transform.SendMessage( "OnBullet",
                 SendMessageOptions.DontRequireReceiver );
         }
     }
     
 }
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 aldonaletto · Dec 13, 2012 at 11:36 PM

You must keep track of how many bullets you have. Usually you count the bullets currently in your weapon: when they reach zero, reload the weapon with a new clip, which contains a fixed and predefined number of bullets. When the bullets in your gun are over, play the reload sound and wait the reload time before actually reloading the bullets variable - like this:

 using UnityEngine;
 
 public class Shooter : MonoBehaviour 
 {
     public AudioClip shotSound;
     public AudioClip reloadSound;
     public AudioClip clickSound; // optional "no bullets" click sound
     public int clips = 2; // how many clips you have
     public int bulletsPerClip = 20; // how many bullets per clip
     public int bullets = bulletsPerClip; // start with a brand new clip in the gun
     public float reloadTime = 0.5f; // reload time in seconds
 
     void Update ( )
     {
         // 1. If Fire button pressed...
         if ( Input.GetButtonDown( "Fire1" ) ){
             if (bullets > 0){ // and you have bullets...
                 Shoot( ); // shoot
             } 
             else // but if gun empty... 
             if (clips > 0){ // and still have ammo clips...
                 StartCoroutine(Reload()); // start reload routine
             }
             else // no bullets, no clips:
             if (clickSound){ // play a click sound, if desired
                 audio.PlayOneShot(clickSound);
             }
         }
     }
 
     private bool reloading = false; // is true while reloading, false otherwise
 
     IEnumerator Reload(){
         // abort other Reload calls if already reloading:
         if (reloading) return null;
 
         reloading = true; // signals that is currently reloading
         clips -= 1; // got one clip, decrement clip count:
         audio.PlayOneShot(reloadSound); // play the reload sound
         yield new WaitForSeconds(reloadTime); // wait the reload time
         bullets = bulletsPerClip; // now the bullets are available
         reloading = false; // reloading finished
     }
     ...
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

10 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

Related Questions

Reload manually in raycast? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 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