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 ThatBroFromFinland · Apr 23, 2015 at 08:45 AM · timewaitforseconds

Wait before being able to press button again

Hi guys! Im new to unity and am trying to do a simple press the mouse shoot projectile wait 5 seconds then your allowed to shoot again. My code here isnt working (I did a seperate script for the time) heres the code:

 IEnumerator Example(){
     if (Input.GetButtonDown ("Fire1")) {
         yield return WaitForSeconds(5) && (Input.GetButtonUp("Fire1"));
         }
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by BroVodo · Apr 23, 2015 at 09:33 AM

 bool canShoot = true;
 
 public float coolDown = 5.0f;
 
 void Update()
 {
     
     if( Input.GetButtonDown ("Fire1") && canShoot )
     {
         
         Shoot();
         
         canShoot = false;
         
         Invoke( "CooledDown", coolDown );
         
     }
     
 }
 
 
 void CooledDown()
 {
     
     canShoot = true;
     
 }

See if that works, might be a bit of overkill?

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 ThatBroFromFinland · Apr 23, 2015 at 10:12 AM 1
Share

it seems rather logical ill check it when i get home from work

avatar image Mordin13 · Aug 04, 2020 at 05:43 PM 1
Share

Amazing, thank you very much! :)

avatar image lauritzarf · Oct 12, 2021 at 06:22 PM 0
Share

Can someone explain what the "CooledDown" meanth and did and came from. I am new to c# and 13 so please don't blaim me for being stupid. Thsnks.

avatar image Hellium lauritzarf · Oct 12, 2021 at 06:25 PM 1
Share

CooledDown is the method called by the Invoke In the condition.

avatar image BroVodo lauritzarf · Oct 13, 2021 at 05:13 AM 0
Share

To elaborate, it is a string parameter for Invoke. The string is the name of the method you want to call. The second parameter of Invoke is the amount of time in seconds that you want to wait before the method (named in the first parameter) to be called.

avatar image
0

Answer by KillMobil · Apr 23, 2015 at 08:56 AM

Input.GetButtonDown ("Fire1") returns True or False and therefore you have to constantly check it with an Update function. There is a great example in Unity documentation that probably does what you want using the input and Time.

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

Comment
Add comment · Show 2 · 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 ThatBroFromFinland · Apr 23, 2015 at 10:12 AM 0
Share

What a fool i have been i searched google and didnt notice this. Alot of thanks!!!

avatar image KillMobil · Apr 23, 2015 at 10:18 AM 0
Share

haha not a fool m8! This was under The Time section that why you may have mist it.

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

Yielding for a changing amount of time? 2 Answers

WaitForSeconds() Is not working. 5 Answers

Gun - Reloading time and reloading animation connected ? 1 Answer

30 Objects all firing at exactly the same time and not randomly 1 Answer

Help Guitar Hero Style or similar 0 Answers


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