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#
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
 }
 
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
     }
 }
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 ();
     }
 }
 
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.
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 :)
Your answer
 
 
             Follow this Question
Related Questions
How to include speed trigger creation option in this script. 0 Answers
Countdown timer... 2 Answers
How can I adjust the speed of an object through a seperate script? 1 Answer
C# cool down not looping 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                