• 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
Question by miguelrmonreal · Jun 27, 2018 at 08:54 PM · cooldownmachinegun

how would i set up a gun that shoots for 3 seconds, then pauses to cool down before it can shoot again?

I'm try to set up a gun that fires a certain amount of shots, then has to cool down before you can start shooting again.

Comment

People who like this

0 Show 0
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

Answer by JVene · Jun 28, 2018 at 02:45 AM

First, the simple case:

In Update (or FixedUpdate, whatever you're using)

Store the return from Time.time in your script's object at the moment the user begins firing the gun. Ensure t$$anonymous$$s is at the start of firing (so you're not resetting t$$anonymous$$s value every update). I like using bools to indicate such a state change, as in "hasFiringStarted" is false until the first trigger press - set it to true, and skip taking a starting time sample when it is true.

When "hasFiringStarted" is true, during update, check the difference between the current time and the start time, as in float dt = Time.time - StartOfFiringTime; When dt >= 3.0f, it's been 3 seconds.

When that happens, you'll need to set some state value that indicates the gun is hot. Perhaps a bool "isGunHot" is set true when dt >= 3.0f. You'll also need to note the time t$$anonymous$$s happens, as in "gunStartsCooling = Time.time;

Now, you repeat about the same t$$anonymous$$ng for gun cooling. How long does the gun have to cool? W$$anonymous$$le gunStartsCooling is true, check the float dt Time.time - gunStartsCooling; when dt >= cooling wait time, you can set "isGunHot" to false.

Now, back toward the gun trigger logic, you in$$anonymous$$bit firing if "isGunHot" is true. When it's false, you can fire.

Comment

People who like this

0 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 JVene · Jun 28, 2018 at 02:47 AM 0
Share

The above can be extended for the not so simple case:

Instead of time, consider temperature. You could add to some temperature value for every fire. Subtract for it during non-firing time (use factors from Time.deltaTime (or Time.fixedDeltaTime if you use FixedUpdate) to control how fast heat drains out of the gun. When it's too hot, inhibit firing.

In this way you're not just limiting a fire of 3 seconds. You're also inhibiting firing if someone fires several 1 second bursts in a row.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

How to have a speed Boost in my game? 2 Answers

When applying a force to an object on Touch, how do I add a *cooldown* to it? 2 Answers

Machine Gun shooting 1 Answer

How make: Timer with Time.time 1 Answer

Problem with hit detection and corountines 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