- Home /
Wait before being able to press button again
Hi guys! Im new to unity and am trying to do a simple press the mouse shoot projectile wait 5 seconds then your allowed to shoot again. My code here isnt working (I did a seperate script for the time) heres the code:
IEnumerator Example(){
if (Input.GetButtonDown ("Fire1")) {
yield return WaitForSeconds(5) && (Input.GetButtonUp("Fire1"));
}
}
}
Answer by BroVodo · Apr 23, 2015 at 09:33 AM
bool canShoot = true;
public float coolDown = 5.0f;
void Update()
{
if( Input.GetButtonDown ("Fire1") && canShoot )
{
Shoot();
canShoot = false;
Invoke( "CooledDown", coolDown );
}
}
void CooledDown()
{
canShoot = true;
}
See if that works, might be a bit of overkill?
it seems rather logical ill check it when i get home from work
Can someone explain what the "CooledDown" meanth and did and came from. I am new to c# and 13 so please don't blaim me for being stupid. Thsnks.
CooledDown
is the method called by the Invoke
In the condition.
To elaborate, it is a string parameter for Invoke. The string is the name of the method you want to call. The second parameter of Invoke is the amount of time in seconds that you want to wait before the method (named in the first parameter) to be called.
Answer by KillMobil · Apr 23, 2015 at 08:56 AM
Input.GetButtonDown ("Fire1") returns True or False and therefore you have to constantly check it with an Update function. There is a great example in Unity documentation that probably does what you want using the input and Time.
What a fool i have been i searched google and didnt notice this. Alot of thanks!!!
haha not a fool m8! This was under The Time section that why you may have mist it.