Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 wyler0 · Apr 17, 2013 at 01:04 AM · timerpowerup

Bug? And any way to do something for 5 seconds when shift is held?

I need to setup a power up system so on shift down the rigid-body on the player will be disabled. Here is the code I have so far. The only problem with this code is when you hit shift in the game.... Frozen freezes up every time.

     void Update () 
     {
         PowerupTimer = Time.realtimeSinceStartup;
         PowerupTimer2 = Time.realtimeSinceStartup + 5;
         if(Input.GetKeyDown(KeyCode.LeftShift) & PowerupUsed == false)
         {
             NoclipPowerup();
         }
     }
     void InstantiatePlatforms()
     {    
          while(NumberOfPlatforms > 0)
          {
              RandNum = Random.Range(0,PlatformList.Count);  //Generates a random number from zero to the total amount of things in our list
              Instantiate(Platform, PlatformList[RandNum].position, Quaternion.identity);
              PlatformList.RemoveAt(RandNum);   //Removes the transform contained at the index we picked at random. You don't want to reuse a spot, since that could put a platform on top of a platform.
              NumberOfPlatforms = NumberOfPlatforms - 1;
          }
         
     }
     void NoclipPowerup()
     {
         while(PowerupTimer < PowerupTimer2)
         {
             Debug.Log("The Powerup works");
         }
     }

Any ideas on the powerup and/or how to fix the crash?

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
0
Best Answer

Answer by iwaldrop · Apr 17, 2013 at 01:43 AM

It's not a bug, you need to use a Coroutine. What you're doing here is sending Unity into an infinite loop because Time.realtimeSinceStartup is always going to be less than Time.realtimeSinceStartup +5. :)

Try this:

 if(Input.GetKeyDown(KeyCode.LeftShift) & PowerupUsed == false)
     StartCoroutine(NoclipPowerup(5));
 
 
 IEnumerator NoclipPowerup(float delay)
 {
     float startTime = Time.time;
 
     collider.enabled = false;
     
     while (startTime + delay > Time.time)
         yield return null;
 
      collider.enabled = true;
 }

But, of course, you don't need to time it yourself, because Unity provides a mechanism for you; here's something a little more elegant.

 IEnumerator NoclipPowerup(float delay)
 {
     collider.enabled = false;
     yield return new WaitForSeconds(delay);
     collider.enabled = true;
 }

You should also consider putting some protection in there to ensure that you only call it once at a time.

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 wyler0 · Apr 20, 2013 at 12:13 AM 0
Share

Thanks a ton! Yea i just realized when viewing your comment that I was putting unity into a loop :P But thanks for the answer! Worked out great and the security check has been put in!

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

12 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

Related Questions

Survival Timer 0 Answers

Trying to Create a Powerup that lasts a certain amount of time 1 Answer

magnetic Powerup Timer Problem 1 Answer

Timer not work :( 0 Answers

Timer running down too quickly 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