- Home /
Change Gun Fire Rate
Hi, I am trying to make a gun shoot 14 bullets a second. I have tried many codes, but the only that will shoot at all. One will shoot one bullet every frame, and the other will shoot one bullet a second. My question is, what should my code be (in Javascript) to make a game object emit 14 bullets a second when I click the primary click button on my mouse.
My codes are:
this one shoots once per frame
var bullet : GameObject;
function Update(){
if(Input.GetAxis("Fire1")){
Instantiate(bullet, transform.position, transform.rotation);
}
}
This one shoots once per click
var bullet : GameObject;
function Update(){
if(Input.GetButtonDown("Fire1")){
Instantiate(bullet, transform.position, transform.rotation);
}
}
Answer by robertbu · Jul 22, 2013 at 04:36 PM
Here is an alternate solution of @cdrandin's that might be simpler for you to work with. It works by having a Shoot function that tries to fire ever 1/14 of a second, but will only fire if 'shooting' is set to true.
#pragma strict
var bullet : GameObject;
var bulletsPerSecond = 14.0;
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 * 1000.0);
}
function Update(){
shooting = false;
if(Input.GetAxis("Fire1")){
shooting = true;
}
}
Note that this solution introduces an average lag between the time the fire button is hit to the shot fired of 0.035 seconds, but I don't think it would be noticed in game play.
thanks, this looks like it makes the most sense, I just need to remove the add force because I already have that component. A lag like this would be about the same as when you squeeze the trigger in real life. Also, what does #pragma strict do?
It disables dynamic typing (i.e. causes the compiler to throw and error). Dynamic typing is much slower than static typing.
http://answers.unity3d.com/questions/206424/pragma-strict.html
I do it for another reason. See this script:
#pragma strict
var someVariable : float;
function Start() {
smoevaribale = 3.14; // Variable misspelled
}
Without the '#pragma strict', this script will compile.
For Robertu's script, can I make the fire rate slower? I adjusted the 14 down but it doesnt do anything.
@TheReclaimer117 - in Javacript, variables are public by default. That means after the script is attached, any changes to the initialization of a variable in script are ignored. You can either select the game object this script is attached to and then adjust the value in the inspector, or you can make the variable private:
private var bulletsPerSecond = 14.0;
Answer by cdrandin · Jul 22, 2013 at 02:17 AM
Make a timer. Allow it to shot fireRate amount of time or you could use corountines, but making your own timer is just as simple.
// Fire gun at the given fireRate.
//This is machine indepedent as it uses Time.DeltaTime
timer += Time.DeltaTime
if(timer > fireRate)
{
gun.fire()
timer = 0 // reset timer for fire rate
}
Set fireRate = 1/14 = .0714. To be a bit more accurate, you would reset the time like:
timer = timer - fireRate; // reset timer for fire rate
how would I make it do this and only do it when I click the mouse?
what am I doing wrong?
var bullet : GameObject;
var fireRate = .0714;
var timer: float;
function Update(){
timer = timer + 1 * Time.deltaTime;
Debug.Log(timer);
if(Input.GetAxis("Fire1")){
if(timer > fireRate){
var inst = Instantiate(bullet, transform.position, transform.rotation);
}
timer = timer - fireRate;
}else{
}
}
Answer by marsfan · Sep 01, 2014 at 05:54 PM
Thank you all of you. The second answer did just what I needed.
Your answer
Follow this Question
Related Questions
Need help with my gun script! 1 Answer
Instantiate New Gun 1 Answer
Ok i got a script i need help on. 0 Answers
Cloned Object does not run Scripts 2 Answers
Bullets not spawning (Instantiate) 1 Answer