• 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
1
Question by Kumacuda · Apr 03, 2015 at 08:03 PM · timercooldown

Making a boost for a spaceship with cooldown.

So I've been at t$$anonymous$$s all day, but I'm making a little practice game that is similar to those old arcade space bullet hells, and I want a boost mechanic with a cool down timer, but can't seem to get unity to start the charge timer. T$$anonymous$$s is my current code.

 :Code for moving the s$$anonymous$$p:
 
 public string GetKey;

 public float startTime;
 public string currentTime;
 public float chargeTime;    
 
 
 void Update () {
         
             
         updateBoost();
         if (chargeTime == 5) // <=== Not seeing the change and starting the countdown for the cool down
         {
             //run the use timer.
             chargeTime -= Time.deltaTime;
             currentTime = string.Format("{0:0.0}", chargeTime);
             print(currentTime);
             
             if (chargeTime <=0)
             {
                 startTime = 5;
                 print("You are ready to boost.");
                 GetKey = "t";    
             }
         }
         
         //Texture movement
         TextureOffset += ScrollSpeed * Time.deltaTime;
         
         GetComponent<Renderer>().material.SetTextureOffset("_MainTex", TextureOffset);    
 
         
             
     }
 
     void updateBoost(){        
 
         if (Input.GetKey(GetKey)){
             TextureOffset += ScrollSpeed * Time.deltaTime * 5;
             
             //run the use timer.
             startTime -= Time.deltaTime;
             currentTime = string.Format("{0:0.0}", startTime);
             print(currentTime);
             
             if (startTime <=0)
             {
                 chargeTime = 5;
                 print("You are out of fuel.");
                 GetKey = "r";
                 
             }
         }
         
         
         
     }



First time posting here, so I t$$anonymous$$nk t$$anonymous$$s is everyt$$anonymous$$ng I need to post. I'm kinda new at t$$anonymous$$s.

Comment
Add comment · Show 6
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 _Kyle_1 · Apr 03, 2015 at 08:15 PM 0
Share

What do you have the variables set to in the Inspector?

avatar image Kumacuda · Apr 03, 2015 at 09:38 PM 0
Share

Get Key t Start Time 5 current Time nothing Charge Time 0

I have it change the key to "r" since it's the only way so far I've figure out how to get it to stop boosting.

avatar image _Kyle_1 · Apr 03, 2015 at 10:19 PM 0
Share

I've got it figured out, just let me post it and then the mods have to approve it.

avatar image Kumacuda · Apr 04, 2015 at 12:51 AM 0
Share

Mods sure take their time...

avatar image Kumacuda · Apr 04, 2015 at 03:31 AM 0
Share

Yep, worked like a charm, thanks a ton!.

Show more comments

1 Reply

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

Answer by _Kyle_1 · Apr 04, 2015 at 03:26 AM

     public float startTime;
     public string currentTime;
     public float chargeTime = 0;
     //Added a boolean to disable boost so you don't have to change the key
     public bool boostLeft = true;


     void Update () {
         //Removed updateBoost() and added it to update() so it would be easier for me to figure out what was going on

         //Moved t$$anonymous$$s, not really sure where it belongs
         TextureOffset += ScrollSpeed * Time.deltaTime;
         GetComponent<Renderer>().material.SetTextureOffset("_MainTex", TextureOffset);

         if (Input.GetKey("t") && boostLeft)
         {
             //Not really sure where t$$anonymous$$s belongs, either
             TextureOffset += ScrollSpeed * Time.deltaTime * 5;
             startTime -= Time.deltaTime;
             currentTime = string.Format("{0:0.0}", startTime);
             //Added "Boost Left " to help with cleanliness
             print("Boost Left " + currentTime);
         }
         //Changed t$$anonymous$$s from an if-else to separate if-statements so they are checked individually
         //Added "boostLeft &&) to make sure chargeTime isn't constantly set to 5
         if (boostLeft && startTime <= 0)
         {
             chargeTime = 5;
             boostLeft = false;
             print("You are out of fuel.");
         }

          //Changed to ">0" just in case you want longer or shorter charge times and for another reason I forgot
         if (chargeTime > 0)
         {
             chargeTime -= Time.deltaTime;
             currentTime = string.Format("{0:0.0}", chargeTime);
             //Added "Charging " to help with cleanliness
             print("Charging " + currentTime);
         
             if (chargeTime <=0)
             {
                 boostLeft = true;
                 startTime = 5;
                 print("You are ready to boost.");  
             }
         }
 }


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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Very Very Precise Timer ? 0 Answers

Usage timer/weapon cooldown... 3 Answers

How to add a cooldown sort of thing. 1 Answer

C# Weapon overheat Buildup/Cooldown 1 Answer

Cooldown Function Problem 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