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
1
Question by Tynan · Jun 05, 2012 at 03:30 AM · updatespeedtimescale

How to run Update multiple times per frame for fast-forwarding purposes

I want functionality similar to games like The Sims where the game can run at 0, 1x, 3x, and ~20x speed.

The first few I can do by adjusting Unity's time scale. But very high speeds don't work this way. Everything breaks as accuracy goes down at high time steps.

I need a way to have Update() run multiple times per frame. So to run at 20x speed I could set the time scale to 2.0 and repeat Update 10 times each frame (or as many as the user's machine can handle).

Does anyone know a good way to set Update to repeat each frame? I know how I could do it by setting up my own version of Update and calling it myself, but I'm wondering if there's a more elegant solution.

Thanks, Tynan Sylvester tynansylvester.com

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by pudd1nG · Jun 05, 2012 at 03:36 AM

You want to use the timescale.

In script, you can simply do

 Time.timeScale = 2.0f


for 2x speed.

In Unity if you goto edit>project settings>time you can set the timescale.

Otherwise just loop over it, you could either call your function twice or simply loop over it. Here's a loop example

 void MyThing(int count){
     for(i=0;i <= count;i++){
         DoThing();
     }
 }

You can then InvokeRepeating on that function, adjust the repeat time and count to find a smooth set of numbers for your timescale & timestep.

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 Tynan · Jun 05, 2012 at 03:41 AM 0
Share

This doesn't answer the question.

avatar image pudd1nG · Jun 05, 2012 at 03:44 AM 0
Share

Well if you don't want the timescale, simply migrate all your Update code into another function and call it twice in update........Or 100 times in update.

avatar image
2

Answer by Berenger · Jun 05, 2012 at 03:38 AM

I'm gonna take an example to make sure I understood your problem.

Let's say you have AI characters using pathfinding to travel into a house in a sims-like game. At timeScale = 1, the delta is of .02, all is fine. With timeScale = 100 however, the delta is 2 and that makes your character miss all the waypoints on your path, problematic with corners and such.

I've never dealt with that before, but try to loop several times inside your Update function, proportionnally to your timeScale. tS of 2, loop of 2, tS of 10, loop of 10 etc. If several scripts need that behaviour, I suggest you create a class between MonoBehaviour and your scripts to deal with that, so you don't have to write it multiple times.

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 Tynan · Jun 05, 2012 at 03:42 AM 0
Share

Yeah I can think of ways to do it with my own custom method. I'm really trying to find the most elegant solution. Creating a subclass of $$anonymous$$onoBehaviour may be that, though I had hoped there was a better way. Thanks.

avatar image Tynan · Jun 05, 2012 at 03:43 AM 0
Share

Hmm, the other issue here is that all the scripts have to run Update in the correct order.

If there are four objects, A, B, C, and D, they have to run ABCDABCDABCDABCD and so on. Not AAAABBBBCCCCDDDD, which I think is what you would get if you just did any variation on looping inside the Update function.

avatar image Berenger · Jun 05, 2012 at 03:48 AM 0
Share

In that case, you might need a master taking care of the updates. it would be a script gathering all the updatable objects (probably any objects bu himself ?), storing them into a list / array. None of them must have an Update function, only the master.

Inside it would alternatively call a custom update function for each of them, once for a timeScale = 1, twice for 2 etc.

avatar image
2

Answer by Tynan · Jun 06, 2012 at 08:15 PM

So here's what I did.

-Created a central time manager class.

-Created a subclass of MonoBehavior called MonoBehaviorGame. All my game actors derive from this.

-MonoBehaviorGame has a ZUpdate() and ZLateUpdate() function.

-In their Start() methods, all MonoBehaviorGames call a function which registers them with the central time manager.

-Every Update() on the time manager, I loop through the list of all registered MonoBehaviorGames and call their ZUpdates(), then their ZLateUpdates().

-Note that between the above loops I run an algorithm that consolidates out any null entries in the list of registered MonoBehaviorGames. This handles actors being destroyed.

Seems to work so far, except a few quicks I have to work out with how the interface such interact with it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase Players Speed Overtime in Endless Runner? Is my Code Unoptimized? 0 Answers

even just cubes moving seems choppy 1 Answer

Help with my "speeding up time" script 1 Answer

Getting Input and do Action Until.. 2 Answers

Slow motion all but one - yet another round, hopefully the last 6 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