- Home /
How to get my ability to deactivate when it's time then not activate till cooldown is reached C#
Good Morning,
Sorry for the terribly worded question XD but I am making a 2D side scrolling Metroidvania and I have an ability to where when the player hits W they will activate a Game Object that will rest on the player for 45 seconds and shot bullets when they shoot (got that figured out). My problem is I can't get it to deactivate and I am running in circles trying to figure it out. Here is the code and related variables, if there is a better way I am all ears
public bool alphaCommand;
public float alphaCommandTimer;
public GameObject alphaCommandSpawn;
public Transform alphaCommandBulletSpawn;
public float alphaCommandCooldown;
public bool canAlphaCommand = true;
if(inWolfForm == true && alphaCommand == false && Input.GetKeyDown(KeyCode.W) && canAlphaCommand == true)
{
alphaCommandTimer += Time.deltaTime;
Debug.Log(alphaCommandTimer);
alphaCommand = true;
canAlphaCommand = false;
if(alphaCommand == true && alphaCommandTimer <= 45)
{
alphaCommandSpawn.SetActive(true);
// Instantiate minion at a transform and make it function like a grunt
// Actually just make it apart of the player and have it set active and when the player fires a bullet it will fire one as well
}
}
else if(alphaCommandTimer >= 45)
{
alphaCommandSpawn.SetActive(false);
alphaCommand = false;
alphaCommandTimer -= Time.deltaTime;
if(alphaCommandTimer <= 0 && canAlphaCommand == false)
{
canAlphaCommand = true;
}
}
Kindly,
Harpoaian
Answer by I_Am_Err00r · Jul 11, 2019 at 03:45 PM
Use a coroutine.
if(inWolfForm == true && alphaCommand == false && Input.GetKeyDown(KeyCode.W) && canAlphaCommand == true)
{
alphaCommandTimer += Time.deltaTime;
Debug.Log(alphaCommandTimer);
alphaCommand = true;
canAlphaCommand = false;
StartCoroutine(Cannon());
}
At the moment you call the cannon write the StartCoroutine line, then type in this block of code at the bottom of your script outside of any functions (this requires you to add a public float to your script here called timeActive, you can set that in the inspector to 45, but this will allow you to adjust it later and use on other objects that might have a different cooldown time):
IEnumerator Cannon()
{
yield return new WaitForSeconds(timeActive);
alphaCommand = false;
yield break;
}
Let me know if that works.
Okay I did what you said and it is kinda working the way I envisioned it. So as of right now here is what I had to do. Right now the object does go away in 45 seconds but I want to make it so when that 45 seconds is up they won't be able to use it again for 90 seconds. After 90 seconds have passed then make it so the player can use the ability again. What can I do to achieve this goal. Scripting isn't my strong suit but I am getting better and better!
$$anonymous$$indly,
Harpoaian
public bool alphaCommand;
public float alphaCommandTimer;
public GameObject alphaCommandSpawn;
public Transform alphaCommandBulletSpawn;
public float cooldown;
public bool canAlphaCommand = true;
public void AlphaCommand()
{
if (inWolfForm == true && alphaCommand == false && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) && canAlphaCommand == true)
{
alphaCommand = true;
canAlphaCommand = false;
StartCoroutine(AlphaCommandOff());
if (alphaCommand == true && canAlphaCommand == false)
{
alphaCommandSpawn.SetActive(true);
}
}
}
IEnumerator AlphaCommandOff()
{
yield return new WaitForSeconds(cooldown);
alphaCommand = false;
alphaCommandSpawn.SetActive(false);
alphaCommandTimer = 0;
canAlphaCommand = true;
yield break;
}
First, setup a bool (I would set it as public in case something else needs to see if coolDown is active) variable called something like coolDown and another public float named something like coolDownTime.
Change the first coroutine I wrote for you to this:
IEnumerator Cannon()
{
if(coolDown == true;)
{
yield return;
}
yield return new WaitForSeconds(timeActive);
alphaCommand = false;
coolDown = true;
StartCoroutine(CoolDown());
yield break;
}
Then you just add this below the first IEnumerator:
IEnumerator CoolDown()
{
yield return new WaitForSeconds(coolDownTime);
coolDown = false;
yield break;
}
Forgot to mention, set the bool as false in Start();
So far so good I just don't know why there is a red line under the new return line you had me put I set the bool in start as well
public bool alphaCommand;
public float alphaCommandTimer;
public GameObject alphaCommandSpawn;
public Transform alphaCommandBulletSpawn;
public float duration;
public float cooldown;
public bool canAlphaCommand = true;
public void AlphaCommand()
{
if (inWolfForm == true && alphaCommand == false && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) && canAlphaCommand == true)
{
alphaCommand = true;
canAlphaCommand = false;
StartCoroutine(AlphaCommandOff());
if (alphaCommand == true && canAlphaCommand == false)
{
alphaCommandSpawn.SetActive(true);
}
}
}
IEnumerator AlphaCommandOff()
{
if(canAlphaCommand == true)
{
// Return has a red line under it
yield return;
}
yield return new WaitForSeconds(cooldown);
alphaCommand = false;
canAlphaCommand = false;
alphaCommandSpawn.SetActive(false);
StartCoroutine(AlphaCommandOn());
yield break;
}
IEnumerator AlphaCommandOn()
{
yield return new WaitforSeconds(duration);
canAlphaCommand = true;
yield break;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Add audio. 1 Answer
Adding cooldown C# 2 Answers
[2D] Add force to all colliders within a certain radius 1 Answer