Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Overrides in LateUpdate having no effect on animation

Discussion in '2D' started by chris1P, Mar 24, 2022.

  1. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    I'm having an issue with animations where in various places I'm trying to override the animations in code and it's having no effect if the Animator is enabled. According to everything I've read in the docs and forums using LateUpdate should mean the values get applied after the animations, but it just doesn't work. I have a really simple test case where I'm setting one bones rotation to the identity transform and it has zero effect. If I disable the Animator component then it works just fine. So its like the animation is being applied after LateUpdate. I don't know if the problem is specific to the 2d-animation package, but this seems like such a basic thing to want to do I can't believe it doesn't work. I've tried changing the update mode on the Animator, and moving the code to Update or FixedUpdate.

    I'm using Unity 2020.3.25 and 2danimation 5.0.10.
     
    Fitbie likes this.
  2. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    This is the entire test script.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Testing : MonoBehaviour
    4. {
    5.     [SerializeField] private Transform m_targetBone;
    6.     private void LateUpdate()
    7.     {
    8.         m_targetBone.rotation = Quaternion.identity;
    9.     }
    10. }
    11.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    This should work, and in fact I just verified that it does. See enclosed package, press Letter O to override.

    Are you modifying something other than what you think you are modifying?

    Are you perhaps modifying the prefab bone rather than the instantiated one??

    Or perhaps this 2D animation system uses different timing... which would seem ... unlikely.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    My test script:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker
    4.  
    5. public class OverrideMeJohnny : MonoBehaviour
    6. {
    7.     public Transform Thingy;
    8.  
    9.     void LateUpdate()
    10.     {
    11.         if (Input.GetKey( KeyCode.O))
    12.         {
    13.             Thingy.position = new Vector3( 4.0f, 1.0f);
    14.             Thingy.rotation = Quaternion.identity;
    15.         }    
    16.     }
    17. }
     

    Attached Files:

  4. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    I imported your package into 2020.3.25 and it totally works.

    It does seem unlikely... and yet I can't think of anything else it could be right now. Another reason I think it might be timing related to the 2D pipeline is that if I change the update mode of the Animator to "Animate Physics" then it seems to jump back and forth between the animated value and what I set in code.
     
  5. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    Verified this is still an issue in Unity 2021.2.16 with 2d animation 7.0.4.

    In fact, I opened up the _Character scene from the samples and attached the script from above (pointed at transform bone_3) and it shows the same issue.
     
  6. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    This is the package for the aforementioned test using the 2d animation character sample.
     

    Attached Files:

  7. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    I think I found a fix, by looking at the source for the 2D IK system. That system also updates rotations in LateUpdate, but it was working. I found that the magic ingredient is:

    Code (CSharp):
    1. [DefaultExecutionOrder(-2)]
    I had actually tried changing the script execution order, but I hadn't tried making it negative. That actually doesn't make sense to me, since now it runs *before* the default update, so I would think that would guarantee that it gets overridden by the animation update if that runs in the default update. But hey it works so yay!
     
    Fitbie and rhys_vdw like this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    That is some extremely good information to know. Thanks for coming back for the postmortem. I will update my animation timing notes to include that.
     
    chris1P likes this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    FYI: I also mentioned the above to the nice animation devs on our team to see if there's some extra info required for the docs etc.
     
    chris1P and Kurt-Dekker like this.
  10. chris1P

    chris1P

    Joined:
    Aug 20, 2013
    Posts:
    27
    Having thought about this a bit more the idea of setting the execution order to -2 makes sense to me. I think the animation system is running before LateUpdate is called on any script, but the sprite renderer (which uses the transforms) runs in the default time. So our script has to modify the transform inbetween the animation system modifying it and the renderer using it.

    Another part of this is that if you are using IK at all, and any of the animations also use IK, then to modify the IK target transform you need the execution order of your script to be less than -2, so it runs after the animator but before the IK solvers.
     
    Kurt-Dekker likes this.
  11. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Thanks for the feedback, @chris1P
    It is great to know your use case and how you solved it. I can see how this can be confusing, especially since we do not have any good documentation or samples to explain the execution order of scripts in the 2D Animation package. I've logged your feedback, so that we can use it in the future to improve the 2d feature sets.
     
    chris1P and MelvMay like this.
  12. s_marcell

    s_marcell

    Joined:
    Mar 22, 2018
    Posts:
    16
    Hi!
    Has there been any updates on this? The above mentioned workaround does solve the problem, but I find it really inconvenient not to mention it took me an hour to stumble upon this solution.
     
  13. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Hello @s_marcell,
    In Unity 2023.1 (2D Animation 10) we have updated the execution order to account for this.
    These are the updated execution orders:
     
    s_marcell, Fitbie and Chubzdoomer like this.