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 El-Deiablo · May 24, 2016 at 02:28 PM · timerpowerupslowmotion

Powerup Timer not Working

I cannot figure out how to make my timer work! If the player collides with the slowdown powerup (slowing all objects on the screen) the slowdown continues forever even though I have a timer setup. Do I have the timer in the wrong spot in Update? Does this have something to do with how I have variables setup and Time.timescale? I have looked on unity answers, youtube, and google for days, but there is nothing. This cannot be that difficult and I am so close! This is driving me crazy!

Here is my code:

Code 1:

 using UnityEngine;
 using System.Collections;
 
 public class PowerUpManager : MonoBehaviour {
 
     private  bool doublePoints;
     private  bool slowMo;
     private  bool fastMo;
 
     private  bool powerUpActive;
     private float powerUpLengthCounter;
 
     private ScoreManager theScoreManager;
 
     private float score;
 
     private float slowDown = Time.timeScale ;
     //private float slowDown = Time.unscaledDeltaTime ;
 
 
 
     // Use this for initialization
     void Start () {
         theScoreManager = FindObjectOfType <ScoreManager > ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (powerUpActive) {
 
             powerUpLengthCounter -= Time.deltaTime;
 
             if (slowMo) {
 
                 //Time.unscaledDeltaTime = slowDown;
                 Time.timeScale = slowDown;
                 slowDown  = 0.5f;
 
                 
             }
         }
 
         if (powerUpLengthCounter <= 0) {
 
             //Time.unscaledDeltaTime = slowDown;
             Time.timeScale = slowDown;
             slowDown = 1f;
             powerUpActive = false;
 
         }
     
     }
 
     public void ActivatePowerUpSlowMotion(bool slow, float time, float effect){
 
         slowMo = slow;
         powerUpLengthCounter = time ;
         slowDown = effect;
 
         powerUpActive = true;
 
     }
 } 

Code 2:

 using UnityEngine;
 using System.Collections;
 
 public class PowerUp : MonoBehaviour {
 
     public bool doublePoints;
     public bool slow;
     public bool fast;
     public float speed=Time .timeScale ;
 
     public float powerUpLength;
 
     private PowerUpManager thePowerUpManager;
 
     // Use this for initialization
     void Start () {
         thePowerUpManager = FindObjectOfType <PowerUpManager > ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "Player") {
             thePowerUpManager.ActivatePowerUpSlowMotion (slow, powerUpLength,speed ); 
         }
 
         gameObject.SetActive (false);
         Destroy(gameObject);
     }
 }




Comment
Add comment · Show 4
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 fabian-mkv · May 24, 2016 at 02:43 PM 1
Share

A few concerns looking at your code:

What is this "slow" variable? In Code 2, it doesn't seem to be initialized, so it'll always be false.

Also, in the following lines:

          Time.timeScale = slowDown;
          slowDown = 1f;

and

              Time.timeScale = slowDown;
              slowDown  = 0.5f;

It seems like you should be setting slowDown first before assigning it to Time.timsScale. This is most likely what's causing it to not revert to a normal time scale.

avatar image El-Deiablo · May 24, 2016 at 07:35 PM 0
Share

I appreciate you getting back to me! I understand what you are saying about switching the order of the Time.timeScale= slowdown and slowdown = (x)f, but why does it need to be in this order?

Also I see the "slow" variable is not initialized. I have that variable there along with fast and double points so that I can check which powerup does what in the inspector. I was watching this tutorial - https://www.youtube.com/watch?v=oQ58w0sx-io. This is confusing because the tutorial only covers double points and invincibility and does not involve altering the timescale. Could I define the slow variable like so: slow=Time.timescale. I won't be home until later to try this out. I have been working on this for a week now and this is one of the last parts I have to figure out for my game.

avatar image fabian-mkv El-Deiablo · May 24, 2016 at 09:08 PM 0
Share

(FYI Post your comment as a reply to my comment, not as a new comment for to your question)

The time scale at which the game runs is set by "Time.timeScale". If you do "slowDown = 1f; Time.timeScale = slowDown;" in that order, you are setting slowDown to 1, and then Time.timeScale to the value of slowDown (which is a value 1). If you do it the other way, namely "Time.timeScale = slowDown; slowDown = 1f;" Then you are setting Time.timeScale to whatever value slowDown was previously set to (which would be 0.5f in your code, if I'm not mistaken), and then you're setting slowDown to 1f, which has no effect on the Time.timeScale, so it has no effect on the game.

avatar image El-Deiablo · May 24, 2016 at 09:59 PM 0
Share

For some reason I cannot mark my comments as a reply. It might have to do with the browser at work.

$$anonymous$$y game slows down and speeds up when the player collides with the powerup, but the game continues to move at the same speed and the "powerUpLength" does not appear to be working.

When you were explaining the switching of Time.timescale=slowDown and slowdown=(x)f - The last sentence in your response says "which has no effect on the Time.timeScale, so it has no effect on the game." Did you mean the effect would not occur in game (because currently the effect does occur, but like I said earlier the timer or powerUpLength is not working)?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JoshDangIt · May 24, 2016 at 11:35 PM

Have you tried using a coroutine like this? (Untested)

 public void ActivatePowerUpSlowMotion(float time, float speed)
     {
         StartCoroutine (SlowMoRoutine (time, speed));
     }
 
     IEnumerator SlowMoRoutine(float time, float speed)
     {
         float duration = time * speed; //Convert the powerup duration to realtime, since WaitForSeconds uses gametime. My math may be off here.
         Time.timeScale = speed;
         yield return new WaitForSeconds (duration);
         Time.timeScale = 1f;
     }

The only downside to this is that there's no way to stop the powerup prematurely. If you don't need to do that this code should work fine.

Comment
Add comment · Show 2 · 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 El-Deiablo · May 25, 2016 at 02:30 AM 0
Share

I like the idea! I was thinking about this as well since IEnumerators allow waitforseconds. However I tried implementing your code into my own and am able to get the slowmo to trigger when player and powerup collide, but again game continues to run at the speed I choose and never stop. I understand how coroutines work, but don't have experience when calling on them in other methods. I got a parser error when trying to call the ActivatePowerUpSlow$$anonymous$$otion(float time, float speed). The error said it was expecting a '.' before time? I tried researching and could not find anything except for mistakes with brackets which all seem to be fine in my code. I was able to build the code creating time and speed references, but ,again, the powerup never stops. Would it be possible to consolidate the code like I was attempting in Code1 or do I still need to use both codes? I really appreciate your help.

Code1:

 using UnityEngine;
 using System.Collections;
 
 public class PWRUP : $$anonymous$$onoBehaviour {
 
     //public float time;
     //public float speed;
 
     public void ActivatePowerUpSlow$$anonymous$$otion(float time, float speed)
     {
         StartCoroutine (Slow$$anonymous$$oRoutine (time, speed));
     }
 
     IEnumerator Slow$$anonymous$$oRoutine(float time, float speed)
     {
         float duration = time * speed;
         Time.timeScale = speed;
         yield return new WaitForSeconds (duration);
         Time.timeScale = 1f;
     }
 
     /*void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "Player") {
 
             ActivatePowerUpSlow$$anonymous$$otion(float time, float speed);
         }
 
 
         gameObject.SetActive (false);
         Destroy(gameObject);
     }*/
 
 }  

Code2:

 using UnityEngine;
 using System.Collections;
 
 public class PowerUp : $$anonymous$$onoBehaviour {
 
     public bool doublePoints;
     public bool slow;
     public bool fast;
     //public float speed;
 
     //public float time;
 
     private PWRUP thePowerUp$$anonymous$$anager;
 
     // Use this for initialization
     void Start () {
         thePowerUp$$anonymous$$anager = FindObjectOfType <PWRUP> ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "Player") {
 
             //the below line of code will not work and says parser error on 'time'
             //I added the public float speed, time above which allowed the code to build, but this is not correct
 
             //thePowerUp$$anonymous$$anager.ActivatePowerUpSlow$$anonymous$$otion(float time, float speed);
 
         }
             
 
         gameObject.SetActive (false);
         Destroy(gameObject);
     }
 }


avatar image JoshDangIt El-Deiablo · May 26, 2016 at 02:53 AM 0
Share

When you destroy the gameobject you destroy the component, therefore stopping the coroutine. I meant for you to put those 2 functions I made in your powerup manager and reference it from your pickup script.

avatar image
0

Answer by Dave29483 · May 25, 2016 at 07:26 AM

After having a quick read, both methods you tried involves either deltaTime or WaitForSeconds(). Both rely on the time scale. Effectively your timer slows down too. You can either scale the wait duration with the time scale so it takes the right amount of time still or use a timer not affected by time scale. i.e. Time.unscaledTime.

Hope this helps.

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 El-Deiablo · May 25, 2016 at 05:32 PM 0
Share

If you look at my initial post I have some code commented out where I was trying to utilize Time.unscaledTime (Code 1: Lines 18, 36, and 46) but, I was unable to get it to work. How can I implement an unscaled timer into my code? Thanks for any help!

avatar image
0

Answer by Kryzarel · May 27, 2016 at 04:01 PM

Hi there, I think Time.realtimeSinceStartup would have the effect you want. It is not affected by Time.deltaTime. Here's a link to its documentation if you wanna read more on it.

I believe something like the following should work (or at least set you on the right track):

 private float powerUpStartingTime;
 
 void Update ()
 {
     if (powerUpActive)
     {
         //calculate how much time has passed since the powerup was activated
         float timePassed = Time.realtimeSinceStartup - powerUpStartingTime;
 
         //if that amount of time is equal to, or bigger than
         //the power up duration, deactivate the power up
         if(timePassed >= powerUpLengthCounter)
         {
             Time.timeScale = 1;
             powerUpActive = false;
         }
     }
 }
 
 public void ActivatePowerUpSlowMotion(float time, float effect)
 {
     //activate the power up
     powerUpActive = true;
 
     //get the duration of the power up
     powerUpLengthCounter = time;
 
     //apply the desired time slow amount
     Time.timeScale = effect;
 
     //save time of power up activation
     powerUpStartingTime = Time.realtimeSinceStartup;
 }


Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to make a loop for PowerUps/Bonus 2 Answers

End slowmotion effect after x seconds 1 Answer

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

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

magnetic Powerup Timer Problem 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