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 Finjitzu · May 21, 2012 at 11:30 PM · crashtimepausetimescaledeltatime

TimeScale = 0 crashes Unity

I recently implemented a Pause menu into my game, just switching the timescale between 0 and 1.

This code causes Unity to crash:

Code:

 function Update(){
  
 if(myTransform.position == Waypoints[currentWaypoint] && currentWaypoint < 3){
             currentWaypoint = currentWaypoint + 1;
             Go();
 }
 }
  
 function Go(){
  
 for(t = 0; t<1;){
     t += Time.deltaTime * speedMod;
         myTransform.position = Vector3.Lerp(myTransform.position,Waypoints[currentWaypoint],t);
         if(myTransform.position != Waypoints[currentWaypoint]){
             yield;                         
         }
  
 }
 }


I understand why it's freezing, it gets stuck in the for loop because deltaTime = 0.

So I've tried changing my pause menu to set timeScale to 0.0001. This helps, but it still gets stuck in the loop and I'm unable to unpause till the loop finishes.

I've also tried to add a break in the loop if the timeScale == 0.0001. This fixes the pausing and unpausing but causes my lerping to stop working if paused.

Can anyone give me a hand with this?

How do I handle deltaTime loops when changing the timeScale? What are my options?

Comment
Add comment · Show 1
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 Bunny83 · May 21, 2012 at 11:56 PM 0
Share

I don't see any reason how this loop should prevent you from unpausing your game... It doesn't matter it the coroutine runs in the background.

1 Reply

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

Answer by Bunny83 · May 21, 2012 at 11:35 PM

Of course it does. Why do you have this if around the yield?

 if(myTransform.position != Waypoints[currentWaypoint]){
     yield;                         
 }

Time.deltaTime will be 0.0 when timescale is 0.0 so this condition never gets true so your for loop loops endless without any change. Just remove the if statement and execute the yield unconditional.

edit
Hmm, actually the condition should be true, but anyway the most important thing in a coroutine is to execute yield. A lot coroutine setups uses a while(true) loop which will never end, but it's ok as long as you call yield, every iteration.

Comment
Add comment · Show 3 · 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 Finjitzu · May 22, 2012 at 01:59 AM 0
Share

Removing the if statement fixes the crashing. Thanks.

Now the objects speed up after the first waypoint. Something wrong with reseting t to 0 when hitting a waypoint me thinks. The if was to break loop if it reaches the waypoint. So im going to try and add break inside the if the position equals current waypoint. Also when paused the things keep moving till they reach their waypoint. Which is kinda weird.

avatar image Bunny83 · May 22, 2012 at 03:54 AM 0
Share

the main problem is that you don't really lerp. The start and end position have to be constant while you lerp. Just save the current position when starting Go:

 function Go(){
     var startPosition = myTransform.position;
     for(t = 0; t<1; t += Time.deltaTime * speed$$anonymous$$od){
         myTransform.position = Vector3.Lerp(startPosition, Waypoints[currentWaypoint], t);
         yield;                         
     }
 }
avatar image Finjitzu · May 22, 2012 at 01:55 PM 0
Share

ha thanks alot, here I thought I was being clever with my program$$anonymous$$g. $$anonymous$$ust be funny to see what people like me come up with on here. thanks again!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause game that not using deltatime for movment 1 Answer

Pause menu... Isn't pausing everything... 1 Answer

How to Appease a pause menu in a game that changes it's time scale? 1 Answer

Accurate timing while using timeScale. 1 Answer

Single Step (pause, resume), or externally clock game loop 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