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
1
Question by jpthek9 · Dec 31, 2015 at 02:43 AM · physicsfixedupdateinterpolationtimestep

Interpolating positions between FixedUpdate frames?

Lockstep Framework uses FixedUpdate () in Unity to match an assumed fixed timestep. At higher rates, FixedUpdate doesn't at all run at a fixed rate but instead runs multiple times in a single frame to make up for a skipped interval. Something like this occurs:

alt text

In the image, blue ticks represent frames at fixed timesteps, red ticks represent render frames (what I want to interpolate to), and green ticks represent when FixedUpdate frames might actually run. Between the 4th and 3rd to last green ticks, a big gap is present which leads to stuttering with linear interpolation.

How is interpolation done for a simulation running assuming a fixed timestep that runs at an unfixed timestep? I.e. Unity's RigidBodies interpolate very well.

completetimestepz.png (10.7 kB)
Comment
Add comment · Show 6
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 Firedan1176 · Dec 31, 2015 at 03:23 AM 0
Share

I've never been aware of this, but what are you trying to interpolate? Why don't you use Unity's Linear Interpolation (Lerp)? $$anonymous$$athf.Lerp, Color.Lerp, Vector3.Lerp, what exactly are you wanting to do?

avatar image jpthek9 Firedan1176 · Dec 31, 2015 at 04:19 AM 0
Share

Sorry for not elaborating. It's interpolation between position. Vanilla linear interpolation (lerp) with an accumulator has stuttering because of the uneven distribution of simulation frames.

avatar image CipherZero jpthek9 · Dec 31, 2015 at 09:35 AM 0
Share

https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/

you might wanna try this

Show more comments
avatar image _dns_ · Dec 31, 2015 at 08:19 PM 2
Share

Hi, I did something similar with some simple custom physics. What I did is compute a nextPhysicsPosition during fixed update but not actually use it for physics behavior. That is, the physics state used by the game is "late" by 1 fixed step (and then next FixedUpdate, set "currentPhysicsPosition" to "nextPhysicsPosition" and compute the next "next" etc...). I also store the current Time.FixedTime in some global lastFixedTime variable.

Then, during Update, I just interpolate from currentPhysicsPosition to NextPhysicsPosition using (Time.time - lastFixedTime)/Time.fixedDeltaTime. It works well when reducing time scale to some very small values to simulate high fps. I guess it's how Unity manages rigidbody interpolation, also buffering next frame collisions events.

One previous comment talks about extrapolation. It should work too but extrapolating values can lead to positions that will be corrected/changed by game events (i.e. player clicking/changing direction etc...).

Also, I've never observed such a big gap between FU and U. I don't think unity would call a U frame with a time greater than the next ideal FU time. In your image, Unity would "realize" that it should call a FU frame ins$$anonymous$$d of a U when the timer is just past the ideal blue tick.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gresolio · Apr 07, 2016 at 10:43 PM

"Fix Your Timestep!" by Glenn Fiedler is a very good article, it helps to understand the game loop in general. One solution is to interpolate between the previous and current physics state based on how much time is left in the accumulator:

 const double alpha = accumulator / dt;
 State state = currentState * alpha + previousState * ( 1.0 - alpha );

But unfortunately we don't have direct access to the FixedUpdate's accumulator in Unity. As _dns_ answered earlier, the (Time.time - Time.fixedTime) is the closest value to it:

 float alpha = (Time.time - Time.fixedTime) / Time.fixedDeltaTime;
 Vector3 lerpState = Vector3.Lerp(previousState, currentState, alpha);

Complete example: Unity_FixedUpdate_Aliasing. Please someone correct me if you know a better way to get the accumulator value from the FixedUpdate loop (it would be nice to look at the source code of Unity, but I don't have such possibility). Recently I've also posted some useful links on the forum (post #71), feel free to read them all :)

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

46 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

Related Questions

Making FixedUpdate fixed-timestep-independent 3 Answers

How to make code in FixedUpdate() independent from fixed timestep? 1 Answer

Raycast drags behind the transform that generates it 1 Answer

Physics behaviour changing with framerate - Is FixedUpdate() actually working properly? 3 Answers

How to smooth physics without interpolation 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