- Home /
Making a boost for a spaceship with cooldown.
So I've been at this 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. This is my current code.
:Code for moving the ship:
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 think this is everything I need to post. I'm kinda new at this.
Get $$anonymous$$ey 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.
I've got it figured out, just let me post it and then the mods have to approve it.
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 this, 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 this 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 this 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.");
}
}
}
Your answer
Follow this Question
Related Questions
Cooldown Function Problem 1 Answer
Free 2d dash ability script! 2 Answers
Cooldown/Timer system (Javascript) 0 Answers
Laser fire time limit 0 Answers
.js Timer to prevent chatflood? 1 Answer