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 Shakti_Singh1 · Jun 21, 2021 at 12:33 PM · waitforsecondscoroutinestime.timescale

WaitForSeconds not waiting for specified time

Hey guys, in the below script I am changing the priority of my cinemachine vcam and then starting a coroutine.

     void KillingCamera() {
     guardKillingDollyCart.m_Speed=1f;
     ActiveCamera=gameSettings.GetComponent<GameSettingsController>().GiveActiveCamera();
     if(ActiveCamera!=null)
     {    
         if(gameSettings.GetComponent<GameSettingsController>().tpp)
         {
            var vcam=ActiveCamera.GetComponent<CinemachineFreeLook>();
            guardKillingCameraController.Priority=vcam.Priority+1;
         }
         else{
             var vcam=ActiveCamera.GetComponent<CinemachineVirtualCamera>();
              guardKillingCameraController.Priority=vcam.Priority+1;
         }
     }
     StartCoroutine(TimeSlowDown());     
 }   

The coroutine currently contains only one WaitForSeconds but I have to filll it with multiple of them to change the Time.timeScale to match my animation.

    IEnumerator TimeSlowDown() { 
   yield return new WaitForSeconds(0.3f);
   Time.timeScale=0.06f;
   print("Time is slowed down");
 }  

The problem is that unity does not wait for 0.3 seconds and changes the value of timeScale almost immediately most of the time. Only once or twice in every five times does it wait 0.3 seconds.
I would be very glad if someone could help me solve the problem or tell me another way of waiting for specific time in scripts.
Thanks in advance.

Comment
Add comment · Show 2
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 DenisIsDenis · Jun 22, 2021 at 04:59 AM 0
Share

Try replacing WaitForSeconds with WaitForSecondsRealtime.

avatar image Shakti_Singh1 DenisIsDenis · Jun 22, 2021 at 09:58 AM 0
Share

I had already tried that but the problem remains same. I want to make my animation in slow motion after 0.3 seconds. Do you know any other way of doing that?
The thing is the slow motion works all the time if I pause the game just as the animation is about to start and then resume it. The slow motion then always starts after 0.3 seconds. Does this suggest anything to you to what the problem maybe?

1 Reply

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

Answer by DenisIsDenis · Jun 22, 2021 at 10:11 AM

In theory, the coroutine can be replaced with simple counters:

 bool isStartTimer;
 float timeToWait = 0.3f;
 float timer;

 void KillingCamera()
 {
     guardKillingDollyCart.m_Speed = 1f;
     ActiveCamera = gameSettings.GetComponent<GameSettingsController>().GiveActiveCamera();
     if (ActiveCamera != null)
     {
         if (gameSettings.GetComponent<GameSettingsController>().tpp)
         {
             var vcam = ActiveCamera.GetComponent<CinemachineFreeLook>();
             guardKillingCameraController.Priority = vcam.Priority + 1;
         }
         else
         {
             var vcam = ActiveCamera.GetComponent<CinemachineVirtualCamera>();
             guardKillingCameraController.Priority = vcam.Priority + 1;
         }
     }
     isStartTimer = true;
 }

 void Update()
 {
     TimeSlowDown();
 }

 void TimeSlowDown()
 {
     if (!isStartTimer) { return; }

     timer += Time.unscaledDeltaTime;
     if (timer >= timeToWait)
     {
         timer = 0;
         isStartTimer = false;
         Time.timeScale = 0.06f;
         print("Time is slowed down");
     }
 } 
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

122 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 avatar image avatar image avatar image avatar image

Related Questions

How to gradually increase difficulty. 1 Answer

Using coroutines to do attack patterns 1 Answer

Can specified frames of an audio track trigger events? 0 Answers

How to decrease gravityScale in period of time? 1 Answer

Mysteries of yield 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