• 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 Dreave · Dec 01, 2011 at 09:31 PM · animationreload

Reload Tutorial?

How would I go about writing a reload sort of script, are there any tutorials on this or could someone explain it to me? I already have a reload animation which is triggered when r is pressed but apart from that I dont know what to do. I need some sort of script which would allow my gun to shoot say 8 times before it has to reload. and then some sort of script which would stop the player from being able to shoot, whilst it reloads. I realize this is all very complex but can anyone help me out?

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 PaxNemesis · Dec 02, 2011 at 05:37 PM

I don't have any tutorial for you, but you might want to try something like this: C#

     int count = 8;
     bool reloading = false;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.R))
             Reload();

         // Should only be able to shoot if we aren't reloading and still have ammo.
         if (!reloading && count > 0 && Input.GetButtonDown("Fire1"))
             Shoot();
     }
 
     void Shoot()
     {
         // Do shooting stuff.
         --count;
 
         // If we used our last shot, then we should reload.
         if (count <= 0)
             Reload();
     }
 
     void Reload()
     {
         reloading = true;
         
         // Do reloading stuff.
         count = 8;
 
         reloading = false;
     }


JavaScript Version:

 var count : int = 8;
 var reloading : boolean = false;
 
 function Update()
 {
     if (Input.GetKeyDown(KeyCode.R))
         Reload();
 
     // Should only be able to shoot if we aren't reloading and still have ammo.
     if (!reloading && count > 0 && Input.GetButtonDown("Fire1"))
         Shoot();
 }
 
 function Shoot()
 {
     // Do shooting stuff.
     count--;
 
     // If we used our last shot, then we should reload.
     if (count <= 0)
         Reload();
 }
 
 function Reload()
 {
     reloading = true;
 
     // Do reloading stuff.
     count = 8;
 
     reloading = false;
 }

Hope it helps :)

Comment
Rebaken-Enterprises

People who like this

1 Show 9 · 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 Dreave · Dec 02, 2011 at 06:41 PM 0
Share

Is this java or C# because in java I have the error "expecting semicolon at end"?

avatar image PaxNemesis · Dec 02, 2011 at 06:45 PM 0
Share

This is C#, and these functions have to be inside a class to work :)

avatar image PaxNemesis · Dec 02, 2011 at 07:05 PM 0
Share

Added the javascript version :)

avatar image Dreave · Dec 02, 2011 at 07:25 PM 0
Share

This all works as it should, but is there any way to stop the gun from shooting once the ammo gets to 0?

avatar image PaxNemesis · Dec 02, 2011 at 07:36 PM 0
Share

Just remove the Reload call in the Shoot function, then you manually have to reload ^^

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reloading Help 2 Answers

Wait for reload to finish? 1 Answer

Stop animation when no more bullets 1 Answer

Loop Animation Help 1 Answer

Animation Scripting Help 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