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
2
Question by Brian-Kehrer · Dec 10, 2009 at 10:06 PM · animationphysics

FixedUpdate after Animation - Physics Loop

It seems that the built in animations play after other scripted FixedUpdate loops when 'Animate Physics' is selected - resulting in funny looking behaviour when moving a character in relation to these objects.

Is there a way to enforce the order animations will play in relation to another FixedUpdate loop? And if not, does it always execute after other FixedUpdate loops?

Since there isn't a function being explicity called, I'm not sure if I can control the call order in the normal way (calling a custom update function via some manager script).

I suppose I could 'fake' it by having the animated graphics lag a frame, but that's a bit funky --- this would be the code for that:

public class AnimatedPhysicsThing : MonoBehaviour {

 public Transform physics;
 private Transform graphics;
 private Vector3 lastPosition;

 private void Awake(){
     graphics = transform;   
 }


 private void FixedUpdate(){
     graphics.position = lastPosition;
     lastPosition = theTransform.position;   
 }

}

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

5 Replies

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

Answer by Brian-Kehrer · Mar 04, 2010 at 01:53 PM

Well, in case anyone was wondering here is how you might implement LateFixedUpdate(), if you need it.

private void FixedUpdate(){
    lateFixedUpdate = true; 
}
private void LateUpdate(){
    if(lateFixedUpdate){
        lateFixedUpdate = false;
        // LateFixedUpdate code here
    }
}

Fundamentally, however, I had the animations lag a frame to solve the aforementioned issues - using similar code to that posted in the question.

I'm not sure I understand why one would want Animated physics objects to move after FixedUpdate, it seems like they should move first, and then run FixedUpdate() on everything else, otherwise the graphics appear to be a frame behind.

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 PatHightree · Nov 17, 2010 at 02:16 PM 1
Share

If you want to do things after each FixedUpdate(), this is not the way to do it. Due to the fact that Update() and FixedUpdate() run at different update rates, you'll miss some FixedUpdate() calls. I worked around the problem by centralizing my scheduling stuff in one script's FixedUpdate() and using the observer pattern to let other scripts perform the needed actions.

avatar image Brian-Kehrer · Nov 18, 2010 at 01:14 AM 0
Share

Yeah, I eventually noticed this too, if your frame rate bogs and you end up with multiple fixed update calls / frame. We've also implemented a global update ordering class.

However since this code (O.P) deals with graphics updating, it is still correct, since no matter how many times fixed update runs, we only care that it has run, not catching each iteration. Great point though.

avatar image
2

Answer by Lucas Meijer 1 · Dec 20, 2009 at 10:46 PM

give LateUpdate a shot: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.LateUpdate.html

Comment
Add comment · Show 1 · 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 Brian-Kehrer · Dec 21, 2009 at 04:35 PM 0
Share

LateUpdate() refers to post-Update. Now that animations can be synchronized with physics (but only after FixedUpdate()), I need LateFixedUpdate(). Surely I could create LateFixedUpdate() from LateUpdate(), but that seems silly.

avatar image
2

Answer by slippdouglas · May 18, 2014 at 01:20 AM

Since WaitForFixedUpdate()-triggered corountines consistently run after every other Behaviour's FixedUpdate() has completed (even when a custom Script Execution Order is set), this will consistently call LateFixedUpdate() in accordance with your use-case:

 // C#
 
 void OnEnable() {
     StartCoroutine("RunLateFixedUpdate");
 }
 void OnDisable() {
     StopCoroutine("RunLateFixedUpdate");
 }
 
 IEnumerator RunLateFixedUpdate() {
     while (true) {
         yield return new WaitForFixedUpdate();
         LateFixedUpdate();
     }
 }
 
 void LateFixedUpdate()
 {
     // do post-all-FixedUpdates stuff here
 }

or

 // UnityScript
 
 function OnEnable() {
     StartCoroutine("RunLateFixedUpdate");
 }
 function OnDisable() {
     StopCoroutine("RunLateFixedUpdate");
 }
 
 function RunLateFixedUpdate() {
     while (true) {
         yield new WaitForFixedUpdate();
         LateFixedUpdate();
     }
 }
 
 function LateFixedUpdate()
 {
     // do post-all-FixedUpdates stuff here
 }

And here's a full test script to confirm the behavior: TestLateFixedUpdate.cs

AFAICT, this still runs before the physics engine— which is probably for the best Adjusting transforms in direct response to the physic engine's adjustments tends to lead to bigger problems.

Comment
Add comment · Show 1 · 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 bennedich · Sep 20, 2014 at 10:00 PM 0
Share

This is the correct answer.

The update order as stated in the docs here is wrong. In Unity 4.5.2f1 yield WaitForFixedUpdate is invoked last in the full physics update loop AFTER internal physics update, OnTriggerXXX and OnCollisionXXX. While OnAnimator$$anonymous$$ove seems to be invoked DURING internal physics update.

Sorry I can't upvote because of too low rep.

avatar image
0

Answer by Ashkan_gc · Mar 04, 2010 at 09:35 PM

take a look at Update Order and as Lucas said use LateUpdate or LateFixedUpdate for what you want.

Comment
Add comment · Show 1 · 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 Brian-Kehrer · Mar 05, 2010 at 02:33 PM 0
Share

The problem is Animated physics objects (moving platforms) seem to move after FixedUpdate, which means the physics is a frame behind the graphics, effectively.

avatar image
0

Answer by Burke · Feb 08, 2011 at 06:43 AM

I have a similar situation.

I have a sensitive bit of gameplay code that needs to: run logic tick anims update physics

Since animation seems to tick between update and lateUpdate, everything is golden under good conditions. I would like to move the logic to fixedUpdate (and eat the cost of updating animation) but I have no way to apply pre and post- anim logic in the fixed case.

I've tried a few work arounds, but it gets really messy if these things are decoupled. Since LateFixedUpdate does not exist, has anyone really implemented a decent animation-dependent physics tick solution?

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 Burke · Feb 08, 2011 at 06:11 PM 0
Share

$$anonymous$$y current work around goes like this:

Late Update (post anim) Set target bones based on terrain following Update I$$anonymous$$ //now it will look right

Fixed Update: state logic, physics, blah SA$$anonymous$$PLE (reset the animation back to before I messed with it) //now it will physics right Terrain Following

I forgot that Sample was an immediate mode access into the animation. All sorted

avatar image Burke · Feb 08, 2011 at 06:11 PM 0
Share

that looked better with my formatting

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

What is the script code on how to trigger a song for the player to Listen to? 2 Answers

how can i make my character run up ramps like in sonic, fancy pants or even line rider? 1 Answer

Pinball problem 1 Answer

animation.Play (MissingComponentException: There is no 'Animation' attached to.. 2 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