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 /
  • Help Room /
avatar image
0
Question by ltrout1999 · Jan 28, 2016 at 09:03 PM · c#powerup

Questions about PowerUps?

I created a basic powerup that will double my speed when I go over it. Now I would like to make the speed only last for 'X' number of seconds and for that PowerUp to not be active for 'Y' number of seconds. How should I go about doing this?

Here is my code:

 void OnTriggerEnter (Collider collider)
         {
             if (collider.CompareTag("PowerUp"))
             {
                 IncreaseSpeed (Speed + Speed);
             }
         }
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
2
Best Answer

Answer by corn · Jan 29, 2016 at 09:58 AM

The best way to do that would be to use Coroutines and WaitForSeconds.

Coroutines are enumerators that can be started by a MonoBehaviour, and are executed separately by the engine, on a frame-by-frame basis. A very convenient feature is the ability of Coroutines to suspend execution for a given amount of time, which is hands down the best way to deal with bonus duration and bonus cooldown.

So what you need to do is :

  • Lock the power up when it is activated

  • Increase speed

  • Wait for X seconds

  • Reset speed

  • Wait for Y seconds

  • Unlock the powerup

     using UnityEngine;
     using System.Collections;
     
     public class Player : MonoBehaviour
     {
         float m_Speed = 10f;
         float m_DefaultSpeedBonus = 10f;
         float m_DefaultPowerUpDuration = 5f;
         float m_DefaultPowerUpCooldown = 10f;
         bool m_PowerUpIsActivable = true;
     
         IEnumerator SpeedPowerUp(float bonus, float bonusDuration, float cooldown)
         {
             // Lock the PowerUp
             m_PowerUpIsActivable = false;
     
             // Activate the speed bonus
             m_Speed += bonus;
     
             // Let it last for bonusDuration seconds
             yield return new WaitForSeconds(bonusDuration);
             
             // Remove the speed bonus
             m_Speed -= bonus;
     
             // Start cooldown
             yield return new WaitForSeconds(cooldown);
    
             // After cooldown is over, unlock the PowerUp
             m_PowerUpIsActivable = true;
         }
     
         void OnTriggerEnter(Collider collider)
         {
             if (collider.CompareTag("PowerUp") && m_PowerUpIsActivable)
             {
                 StartCoroutine(SpeedPowerUp(m_DefaultSpeedBonus, m_DefaultPowerUpDuration, m_DefaultPowerUpCooldown));
             }
         }
     }
    

You can apply the same logic to any PowerUp you want to implement.

Comment
Add comment · Show 1 · 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 ltrout1999 · Jan 29, 2016 at 03:55 PM 0
Share

Very helpful! Thanks :)

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

71 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

Related Questions

Collider disabling by itself... 2 Answers

How to make a POWER UP that slows the player down with force. 0 Answers

2D Game powerup script not working at all 0 Answers

2D Power up script not working 0 Answers

How can I separate my Code efficiently ? 0 Answers


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