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
0
Question by low_hanging_nuts · Jul 27, 2016 at 05:58 PM · javascripttime.deltatimecooldown

Adding a cooldown/limited use to my script (Trigger Warning: Javascript)

So, I have a script that spawns a rigidbody directly in front of me, and it spawns it as fast as I can press the button with unlimited ammo. I use this script to spawn in shadow clone A.I. of my character, health boxes that the player can throw down like in Battlefield, and other random abilities that I think of on the fly.

However, I don't want to be able to throw down an infinite amount of things. I want to be able to throw down maybe one or two every couple seconds. I tried doing this with Time.time and Time.deltaTime, however I couldn't get either of those things to work. I also want to make it into a float so that I can change it in editor if at all possible (however that isn't a necessary trait)

I am pretty new to scripting, and I am learning and following tutorials at the same time, and not all of it is in c#. Sorry for the inconvenience!

Here is the code:

 #pragma strict
 
 var theBullet : Rigidbody;
 var Speed = 20;
 var coolDown : float;
 
 function Update () {
     if (Input.GetKeyDown("f"))
     {
         var clone = Instantiate(theBullet, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
 
         Destroy (clone.gameObject, 3);
     }
     if (Input.GetKeyDown("f") && coolDown == 0) 
     {
         coolDown = 2f;
     }
         if (coolDown > 0) 
         { 
             coolDown -= Time.deltaTime; 
         }
 }


At this point I will accept any help I can get. Anything and everything will be much appreciated!

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

Answer by Cepheid · Jul 27, 2016 at 11:04 PM

Hi @low_hanging_nuts

The general premise of your cool-down code is correct you've just made some simple flaws.

In this section:

 if (Input.GetKeyDown("f") && coolDown == 0)
 {
       coolDown = 2f;
 }

You are checking for a key press and for the cooldown to be 0. This will fail because your cooldown counter is counting past 0 and is in fact going into negative numbers. Due to this, your condition will only become true for a very few milliseconds before it becomes false again.

There are two ways to fix your code in the current format.

One of them is to simply check if your cooldown has gone below 0. Like so:

 if (Input.GetKeyDown("f") && coolDown <= 0)
  {
        coolDown = 2f;
  }

Another way would be to simply check if the cooldown has gone below 0. And then set it to zero. Like so:

 if (Input.GetKeyDown("f") && coolDown == 0) 
 {
       coolDown = 2f;
 }
 
 if (coolDown > 0) 
 { 
        coolDown -= Time.deltaTime; 
 }
 else
 {
       coolDown = 0;
 }

Apart from these. Your other issue is due to this statement:

 if (Input.GetKeyDown("f"))
 {
       var clone = Instantiate(theBullet, transform.position, transform.rotation);
       clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
  
       Destroy (clone.gameObject, 3);
 }

In the above statement. You are checking for a key press only. So, regardless of what the coolDown rate is. This statement will always activate as soon as the 'f' key is pressed. You will want to place the logic from this conditional into you're fixed if check. Like so:

 if (Input.GetKeyDown("f") && coolDown <= 0)
  {
        var clone = Instantiate(theBullet, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
   
        Destroy (clone.gameObject, 3);
 
       coolDown = 2f;
  }

I hope that explains it and helps. :)

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

89 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

Related Questions

How do I respawn something after it's been disabled? 0 Answers

I need help with this script. Can't figure out my it isn't working.,Can't get this script to work 0 Answers

How to enable a script in Play-Mode? 1 Answer

Convert a java script in a c#??? thank you 1 Answer

Help with bullet going in the dirrection fired from view of camera?? 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