Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by harpoaian · Jul 11, 2019 at 03:11 PM · c#2d-platformercooldown

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image harpoaian · Jul 11, 2019 at 04:14 PM 0
Share

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;
 }

avatar image I_Am_Err00r harpoaian · Jul 11, 2019 at 05:03 PM 0
Share

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();

avatar image harpoaian I_Am_Err00r · Jul 11, 2019 at 05:26 PM 0
Share

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;
     }

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges