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!
Answer by Cepheid · Jul 27, 2016 at 11:04 PM
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. :)
Your answer
Follow this Question
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