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 BenIV · Apr 13, 2017 at 08:14 PM · c#timewaitforsecondstime.deltatimeyeild

Confused about waitforsconds and need help with making slowmotion only last for a certain number of seconds and then cool down

So I made a slow motion button, If you press E, time goes to .35 and stuff and then you press T to make it go back to normal (1). I want to make it so when you press E, it makes the game slow to .35 FOR 7 SECONDS and then go back to normal and then you have to wait 60 seconds (OR ON ESCAPE) for it to be available again. My script for the slowmo is this: (C# and the name is PauseGame1) using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PauseGame1 : MonoBehaviour
 {
 
 
     void Update()
     {
         SlowMo();
     }
 
 
     void SlowMo()
     {
         if (Input.GetKey(KeyCode.E))
         {
             Time.timeScale = 0.35f;
             Time.fixedDeltaTime = 0.02F * Time.timeScale;
         }
 
 
       if (Input.GetKey(KeyCode.T))
         {
             Time.timeScale = 1;
             Time.fixedDeltaTime = 0.02F * Time.timeScale;
        
 
         }
        else if (Input.GetKey(KeyCode.Escape))
         {
             Time.timeScale = 1;
             Time.fixedDeltaTime = 0.02F * Time.timeScale;
         }
     }
 
 }

Thanks!

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

2 Replies

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

Answer by Davirtuoso · Apr 17, 2017 at 12:19 PM

@BenIV One way would be to make use of Coroutines. They are a nice way of counting time and then performing an action afterwards. This is done through the yield command, and would be done like so:

 if (Input.GetKey(KeyCode.E))
          {
              Time.timeScale = 0.35f;
              Time.fixedDeltaTime = 0.02F * Time.timeScale;
 
              StartCoroutine(WaitThenCool(7.0f));
          }

Then have your seperate Coroutine in an IEnumerator like so:

 IEnumerator WaitThenCool(float waitTime)
 {
       yield return new WaitForSeconds(waitTime);
       // Cooldown script here
 }

The only issue is that this time is influenced by Time.timeScale, which means that 7 seconds of wait time here is actually 7 extended seconds in your time-slowed world, This leaves you with two options:

  1. Work out how long 7 seconds is in your time-slowed world and use that (I think it's 2.45, but not sure), or

  2. Rewrite the yield to instead use Time.realtimeSinceStartup to count actual real-world seconds, though it would be slightly more unfriendly.

Of those options, 1 would be the simplest to implement, though you'd need to consider future repercussions if you need to do the same thing again and write a new method that waits another specific amount of time adjusted by the slowed-world issue. 2 would be slightly more unfriendly to write, though once done you could use the same method for any number of waits providing you tell it what to cooldown afterwards.

As for the actual cooldown, you may already have ideas on this, but just in case a simple way of handling it would be having a boolean value, something like 'CanUse', then wrap all your E input code in whether or not this is true. When the ability is cooling down, it would set 'CanUse' to false, wait 60 seconds and then set it to true again. This could also be done using Coroutines so it's more of what i've already said just adjusted.

Hopefully this helps - good luck!

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 ChompIV · Apr 22, 2017 at 10:37 PM 0
Share

How would I code the CanUse thing?

avatar image Davirtuoso · Apr 23, 2017 at 06:00 PM 0
Share

Well first you'd need to define the variable at the top of your class. Something like:

 bool CanUse = true;

After that you then put an if statement that checks if CanUse is true, only running the keypress check within it, like so:

 If (CanUse == true)
 {
           if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
           {
               Time.timeScale = 0.35f;
               Time.fixedDeltaTime = 0.02F * Time.timeScale;
               CanUse = false;
               StartCoroutine(WaitThenCool(2.45f));
           }
 }

Then within your 'WaitThenCool' coroutine you can call another coroutine the handle the cooldown for you. There is most likely a more efficient way to do this, but as I don't think a single coroutine can be paused twice this is a simple way around it:

 IEnumerator WaitThenCool(float waitTime)
  {
        yield return new WaitForSeconds(waitTime);
        Time.timescale = 1;
        StartCoroutine(CooldownTimeSlow());
  }
 
 IEnumerator CooldownTimeSlow()
 {
      yield return new WaitForSeconds(60.0f);
      CanUse = true;
 }

That would then give you code that checks if CanUse is true. If it is, then it waits for a keypress on the E key. When pressed, it sets the timescale to 0.35, sets CanUse to false so the player can't press the key again, and waits for 7 seconds before setting it back to 1. It then runs 'CooldownTimeSlow()' waits 60 seconds, before setting CanUse back to true, allowing the use of the ability again.

avatar image
2

Answer by Bilelmnasser · Apr 25, 2017 at 01:17 PM

Game Speed can be changed via : Time.timeScale

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

8 People are following this question.

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

Related Questions

Wait time after coroutine's wait seconds is complete 0 Answers

Having trouble increasing the spawning speed of explosions 0 Answers

Calculating distance travelled. Not sure what i'm missing.... 1 Answer

How to skip WaitForSeconds? 1 Answer

-= Time.deltaTime 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