• 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 marsfan · Jul 22, 2013 at 01:26 AM · instantiategungunfirefire rate

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);
     }
 
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

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 marsfan · Jul 22, 2013 at 05:23 PM 0
Share

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?

avatar image robertbu · Jul 23, 2013 at 03:45 AM 0
Share

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.

avatar image TheReclaimer117 · Jan 17, 2014 at 01:32 AM 0
Share

For Robertu's script, can I make the fire rate slower? I adjusted the 14 down but it doesnt do anything.

avatar image robertbu · Jan 17, 2014 at 02:01 AM 0
Share

@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;
avatar image
3

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
 }
Comment
Add comment · Show 5 · 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 marsfan · Jul 22, 2013 at 02:26 PM 0
Share

how do I make it shoot 14 times a second.

avatar image robertbu · Jul 22, 2013 at 02:34 PM 0
Share

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
avatar image marsfan · Jul 22, 2013 at 02:36 PM 0
Share

how would I make it do this and only do it when I click the mouse?

avatar image marsfan · Jul 22, 2013 at 04:23 PM 0
Share

do I use function Update()?

avatar image marsfan · Jul 22, 2013 at 05:20 PM 0
Share

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{
     }
 }
 
avatar image
0

Answer by marsfan · Sep 01, 2014 at 05:54 PM

Thank you all of you. The second answer did just what I needed.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

Need help with my gun script! 1 Answer

Reload clips on Gun-script not applied properly 1 Answer

Setting bullet instansiate direction? help? 1 Answer

how to create a gun in unity3d 2 Answers

Clickable GameObjects within one another... 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges