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 superpentil · Jul 11, 2012 at 02:06 AM · instantiatebulletgunmultiplefire

Make a Gun fire multiple times

I have a gun and need it to fire when I am holding down the left mouse button until a specific number of bullets fired is reached, then i reload. The script I have right now Instantiates a sphere and adds a force to it, but its only a one shot and doesnt have the bullet limit or reload function. I can add the reload and bullet limits myself i think, but im really stuck on the fire rate.

Comment
Add comment · Show 1
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 $$anonymous$$ · Jul 11, 2012 at 02:12 AM 0
Share

Add a timer that resets each time you fire. Only fire if the timer has reached a certain point. Change the time on the timer to change the rate of fire.

3 Replies

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

Answer by DayyanSisson · Jul 15, 2012 at 02:10 AM

Look at this:

http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html

Gives you the actual firing code too.

Comment
Add comment · Show 1 · 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 superpentil · Jul 15, 2012 at 03:18 AM 0
Share

Hey thanks!

avatar image
1

Answer by Muuskii · Jul 11, 2012 at 02:19 AM

I tend to use Time.deltaTime and Mathf.Max like so:

 float timeTillNextShot = 0;
 
 void Update()
 {
 timeTillNextShot = Mathf.Max(timeTillNextShot - Time.deltaTime, 0);   //never go below 0
 
 if(timeTillNextShot <= 0)    //Add condition to check if we're firing here
 {
 timeTillNextShot = timeBetweenShots;   //reset
 fireAShot();
 }
 }


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
0

Answer by fipps129 · Jul 15, 2012 at 12:32 AM

Just use a bool check with InvokeRepeating().

 public bool mousePressed = false;
 public float timeBetweenShots = 0.2f;
 public int shotsPerClip = 30;

 private bool runOnce = false;

 void Update()
 {
    if(Input.GetMouseDown(0)){
      mousePressed = true;
    }
    if(Input.GetMouseUp(0)){
      mousePressed = false;
    }

    if(mousePressed){
      if(shotsPerClip>0&&!runOnce){
        InvokeRepeating("ShootGun", 0, timeBetweenShots);
        runOnce=true;
      }
    }else{
      CancelInvoke();
      runOnce=false;
    }
 }

 public void ShootGun(){
   //do all shooting functions
   if(shotsPerClip<=0){
     CancelInvoke();
     //call reload function
   }
 }



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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting bullet instansiate direction? help? 1 Answer

Bullets aren't firing / Bullet holes not instantiating correctly 0 Answers

Control amount of bullets 2 Answers

Best way to make a gun? 1 Answer

Bullets Based on Orientation 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