- Home /
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
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.
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
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.
I appreciate it. I've actually been watching some tutorials at Unity3Dstudent.com starting from the very basics so I can learn. Thanks.
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
![](https://koobas.hobune.stream/wayback/20220613130648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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