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
0
Question by davidflynn2 · Oct 27, 2012 at 06:10 AM · bullets

Preventing Rapid Fire

When I press the button for my weapon fire I can spam it firing bullet after bullet, I would like to limit this to add like a 2 second delay in bettwen shots and I am not sure how to add this in.

This is my fire code.

 var missile : GameObject;
 var missile2 : GameObject;
 var missile3 : GameObject;
 
 
 function Update () {
     if (Input.GetMouseButtonDown (0))
     {
         var position : Vector3 = new Vector3(0, 0, 0) * 10.0;
         position = transform.TransformPoint (position);
         var thisMissile : GameObject = Instantiate (missile, position, transform.rotation) as GameObject;
         //Physics.IgnoreCollision(thisMissile.collider, collider);
         
         var position3 : Vector3 = new Vector3(0, 0, 0) * 10.0;
         position = transform.TransformPoint (position3);
         var thisMissile3 : GameObject = Instantiate (missile3, position3, transform.rotation) as GameObject;
     }
     
         else if (Input.GetMouseButtonDown (1))
     {
         var position2 : Vector3 = new Vector3(0, 0, 0) * 10.0;
         position = transform.TransformPoint (position2);
         var thisMissile2 : GameObject = Instantiate (missile2, position2, transform.rotation) as GameObject;
         //Physics.IgnoreCollision(thisMissile.collider, collider);
     }
     
 
     
     
 }
 
Comment
Add comment · Show 2
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 Fattie · Oct 27, 2012 at 09:20 AM 1
Share

there is a massive discussion about basic timer matters on unityGE$$anonymous$$S.com

hope it helps

BTW never use "else if"

your best pattern there is if .. { blah return }

never use else if.

avatar image tavoevoe · Oct 27, 2012 at 05:50 PM 0
Share

What's wrong with else if?

2 Replies

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

Answer by Jessespike · Oct 27, 2012 at 06:04 PM

A timer can work here.

 var shootCooldown : float = 2;    // Wait time between shots
 var shootTimer : float;            // Tracks the time since last shot fired

 function Update () {
     shootTimer += Time.deltaTime;       // Keep track of passing time
     if (shootTimer < shootCooldown) {
         return;                         // Gun not ready to fire, prevent firing.
     }


     if (Input.GetMouseButtonDown (0))
     {
        shootTimer = 0;                  // Reset timer
 
        var position : Vector3 = new Vector3(0, 0, 0) * 10.0;
        ...
 
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
avatar image
-1

Answer by xtremepman · Oct 27, 2012 at 05:09 PM

use invoke

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Multiple Cars not working 1 Answer

Make GUI.TextArea show up only when Left Ctrl pressed 1 Answer

A node in a childnode? 1 Answer

Noobish Question Pertaining to JS Movement 2 Answers

How to fire two bullets instead of one? 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