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
1
Question by TheReclaimer117 · Jan 17, 2014 at 02:59 AM · javascriptammoreloading

How do I add ammo and reloads?

My gun is a sci-fi one and in a sense "Reloads itself" but what can I do to add ammo and reloading. I don't need an animation now just reloading, and what button will it use? I'd like something like... 64 rounds in a clip, and reloading is 3.5 seconds long. Also to make the gun stop firing when ammo = 0. Here's my script.

 #pragma strict
  
 var bullet : GameObject;
 
 private var shooting = false;
  
 function Start() {
    InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
 }
  
 function Shoot() {
   if (!shooting) return;
   var go = Instantiate(bullet, transform.position, transform.rotation);
   go.rigidbody.AddRelativeForce(Vector3.forward * 1750.0);
 }
  
 function Update(){
     shooting = false;
     if(Input.GetAxis("Fire1")){
         shooting = true;
     }
 }
 private var bulletsPerSecond = 6.0;

Thanks. I'm in Javascript by the way

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
1
Best Answer

Answer by xt-xylophone · Jan 17, 2014 at 03:45 AM

Reloads are pretty simple. Have 3 more variables, one being the max bullets in the clip, another being the current bullets in the clip and finally the duration of the reload in seconds.

At the start just set the current to the max so you only need to set 1 number and it starts both. Each time you shoot take one off the current number. Only shoot if current number is greater than 1. Now on some other button press use an Invoke function, look up Unity docs how. Call some simple function with it after your reload timer in seconds, in that reload function just set current clip to max clip.

Come to think of it you may need a boolean keeping whether you are reloading or not which you set to true when you press the reload key and false when its done. In the shoot part of the script add another condition that you cant shoot while reloading.

And your done!

Id also recommend not using a background invoke repeating to do your shooting.

Comment
Add comment · Show 4 · 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 TheReclaimer117 · Jan 17, 2014 at 05:32 AM 0
Share

Thanks. I'm learning Javascript but I still can't really produce much. The code I showed above is someone else's that I modified by adding a line and changing some numbers. Do you know where I can get a script for those 3 lines you recommended? Thanks for the answer though

avatar image xt-xylophone · Jan 19, 2014 at 10:43 AM 0
Share

Its not just 3 lines to this, it can be done in almost anyway you want. Read some more program$$anonymous$$g tutorials then come back and read my reply and you will be able to do it yourself no probs :)

Dont want to just give away code of this level. If it were some more complex code then yeah I will give straight examples so giving you this won't help you learn the basics.

avatar image TheReclaimer117 · Jan 19, 2014 at 11:00 AM 1
Share

I appreciate it. I've actually been watching some tutorials at Unity3Dstudent.com starting from the very basics so I can learn. Thanks.

avatar image xt-xylophone · Jan 20, 2014 at 04:01 AM 1
Share

Just play around too, you can't permanently break your code, you can always start again and try new things.

Also error messages are your friend, work through them one by one 1st to last as you solve each one.

Trying things and learning how and why they failed is the fastest way to learn. Read my reply again in a couple weeks and it may suddenly click how you need to do it.

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

19 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 avatar image avatar image avatar image avatar image

Related Questions

I Made a Revolver Script, But It's Saying There is an Error 2 Answers

reloading? 1 Answer

Loading next level, but next level never gets incremented. 3 Answers

Disable script from code 5 Answers

Super mario Camera Script 0 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