Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by iLostCause · Sep 28, 2016 at 04:29 PM · scripting problemfpswaitforsecondsunity 4.6functions

Using waitforseconds to put delay between gunshots

HELP! I'm using unity 4.6.9, and I have a script to make a gun shoot. It works perfectly, except you can fire as fast as you want, as in several bullets per second. I want to have like a one second delay between the shots that you can fire. However, after trying again and again to get a yield waitforseconds (5); to work, I haven't been able too. The script below does nothing. Or rather, it does exactly as it does normally, with absolutely no delay. I've even tried ramping the delay up to 100, and it still doesn't do anything. I've been trying for days!!!

 var projectile : Rigidbody;
 var speed = 10;
 
 function wait() {
     yield WaitForSeconds(1);
 }
 function Update () {
     if(Input.GetMouseButtonDown(0)) {
         clone = Instantiate(projectile, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
         audio.Play();
         Destroy (clone.gameObject, 5);
     }
     wait();
 }
 

Please help!!!

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

1 Reply

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

Answer by TBruce · Sep 28, 2016 at 04:55 PM

This will work for you

 var projectile : Rigidbody;
 var speed = 10;

 // the time allowed between shots (in seconds or parts thereof)
 // the smaller the value the faster shots can occur
 var timeBetweenShots = 1;
 
 // flag to determine if a shot was fired
 private var shotFired = false;
 
 function FireShot()
 {
     // fire the shot
     clone = Instantiate(projectile, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
     audio.Play();
     Destroy (clone.gameObject, 5);

     // do not allow another shot until timeBetweenShots has elapsed
     yield WaitForSeconds(timeBetweenShots);

     // reset the shot flag so shooting can resume
     shotFired = false;
 }
 
 function Update ()
 {
     if ((!shotFired) && (Input.GetMouseButtonDown(0)))
     {
         // immediately set the shot flag so another shot can not be made
         shotFired = true;
         FireShot();
     }
 }
Comment
Add comment · Show 3 · 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 iLostCause · Sep 28, 2016 at 08:32 PM 0
Share

Thank you so much!!! This worked perfectly!

avatar image TBruce iLostCause · Sep 28, 2016 at 08:35 PM 1
Share

I am happy that this is working for you. Would you be so kind as to click the "Accept" button above to accept the answer? Thank you!

avatar image iLostCause TBruce · Nov 09, 2016 at 01:52 AM 0
Share

Happily! That script was bothering me for ages!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

StartCoroutine not listening to parameters 1 Answer

How do I assign values to variables in the Update function only ONCE? 1 Answer

BasicFPS controller "Can not be loaded" 0 Answers

When player enters area. Object that script is attached to walks toward the entering object(player). 1 Answer

UNITY shooting script does nothing 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