• 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 Sev077 · Sep 07, 2015 at 03:48 AM · c#timerspeedcooldownthrust

How to have a speed Boost in my game?

I have my 2d game. The player can move and then a button is pressed they have a speed boost. How can I make it where the speed boost only works for 2 seconds and make the player wait 5 seconds before using it again? Also how would I add a bar to show the player when they can use it again? I'm using c#

Comment
Add comment · Show 1
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 dr3th · Sep 07, 2015 at 02:54 PM 0
Share

You want some basic function structure as below?

 //InitialiseTheTimeOnCreationOfTheProgram
 DateTime TimeSinceLastBoost= DateTime.Now.Add(-5);
 
 public void Boost(){
 //IfLastBoostTimeWasOver5SecondsAgo
     //BoostPlayersSpeedFor2Seconds
     //RecordBoostTime
 }
 
 //Bind this output to the Bar Length
 public  int BarWhenCanPlayerBoostAgain()
 {
       // return TheDifferenceBetweenTimeSinceLastBoostAndCurrentTime
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Suddoha · Sep 07, 2015 at 03:30 PM

Coroutines work well for that purpose, so you might be looking for something along these lines:

 public class BoostExample : MonoBehaviour
 {
     public float boostCooldown = 5f;
     public float boostDuration = 2f;
     private float speedBoost = 3;
 
     private bool hasCooldown;
     private Vector3 normalMovementVector = Vector3.forward;
     private Vector3 currentMovementVector;
 
     void Start()
     {
         currentMovementVector = normalMovementVector;
 
         // doesn't allow to have speed right at the beginning
         // but comment it out if you want to have boost immediately at startup
         StartCoroutine(ActivateCooldown());
     }
     
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space) && !hasCooldown)
         {
             // apply boost, i simply added another vector to it
             currentMovementVector += Vector3.forward * speedBoost; 
             // activate the cooldown and start the deactivation method for the boost
             StartCoroutine(ActivateCooldown());
             StartCoroutine(ResetMovementVector());
         }
         // just some basic movement for the test
         transform.Translate(currentMovementVector * Time.deltaTime);
     }
 
     IEnumerator ResetMovementVector()
     {
         // wait some seconds
         yield return new WaitForSeconds(boostDuration);
         // return to normal speed
         currentMovementVector = normalMovementVector;
         Debug.Log("boost ended");
     }
 
     IEnumerator ActivateCooldown()
     {
         // put some code to disable the boost-is-ready bar
         // diable the ability to use boost
         hasCooldown = true;
         // wait until the boost is ready again
         yield return new WaitForSeconds(boostCooldown);
         // make the boost usable
         hasCooldown = false;
         Debug.Log("boost ready"); 
         // put some code to enable the boost-is-ready bar
     }
 }

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

Answer by bubzy · Sep 08, 2015 at 06:47 AM

I'n not very good at articulating answers so i wrote a small script to show how it could be done, there are probably better ways, the variables are public so that you can see them in the editor.

 using UnityEngine;
 using System.Collections;
 
 public class boost : MonoBehaviour {
     public float boostTime = 2.0f;
     public float currentBoostTime;
     public float boostDelayTime = 5.0f;
     public float currentBoostDelayTime;
     public bool boosting = false;
     public float time;
 
     public float baseSpeed = 1.0f;
     public float speedBoost = 2.0f;
     public float speed;
 
     // Use this for initialization
     void Start () {
         currentBoostTime = 0f;
         currentBoostDelayTime = 0f;
         speed = baseSpeed;
     }
 
     void movePlayer()
     {
         if (Input.GetKeyDown(KeyCode.Q) && !boosting && Time.time > currentBoostDelayTime) { //or whatever your boost button is
             currentBoostTime = Time.time + boostTime; //start the timer for the boost
             boosting = true;
         }
 
         if ((Time.time > currentBoostTime) && boosting) { // am i boosting? has the boost timer expired?
             boosting = false;
             currentBoostDelayTime = Time.time + boostDelayTime;
         }
 
         if (boosting) {
             speed = speedBoost;
 
         }
             
         if (!boosting) {
             speed = baseSpeed;
 
         }
     }
 
 
     // Update is called once per frame
     void Update () {
         time = Time.time; //debug
         movePlayer ();
     }
 }
 

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 bubzy · Sep 08, 2015 at 06:48 AM 0
Share

oh didnt see you wanted a bar also, going to work now, if you havent worked it out by the time i get home, ill post a solution for that too :) have fun.

avatar image bubzy · Sep 08, 2015 at 09:36 PM 0
Share

ohhhhh, forgot. i havent really used the new GUI system, so i found a tutorial of a guy making a health bar, should be easily transferable to the boost system :)

Tutorial Here

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Play Audio with Timers 0 Answers

I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer

HH:MM:SS game clock with modifiable start time error 1 Answer

Timer float not evaluating correctly when I check if it's less than another number 3 Answers

How can I increase the speed gradually of each instantiated clone. 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