- Home /
When do constraints, like ParentConstraint and AimConstraint, evaluate?
I frequently employ the LateUpdate method on MonoBehaviours to locate or modify animated Transforms, after their animations have been evaluated. This works with both Timeline and Mecanim state machines, and has any number of uses; such as temporarily pinning props to character's hands, or offsetting the rotation of a character's bones.
I have recently taken to using the AnimationModule's new Constraints, such as ParentConstraint and AimConstraint. They're fantastic, but I have just discovered that they do not evaluate in time for their output to be used in LateUpdate. If I query the position of a constrained Transform in LateUpdate, it appears to return its position from the previous frame, not unlike querying an animated Transform's position in Update.
The effect is fairly catastrophic, in that the position of any Transform, with a Constraint anywhere above it in the hierarchy, is more or less impossible to find. Even using WaitForEndOfFrame to retrieve the value inside a Coroutine seems to yield an incorrect result.
When do Constraints actually evaluate, and is there any way to get the current or final position of a Transform being driven by a constraint? Constraints have been such a delight to work with otherwise, I would hate to have to stop using them. Any help would be much appreciated.
As an example of why one might need to know the position of a constrained Transform; I have a Spline system where a number of Transforms can serve as the vertices of a Bezier Curve. The resulting Spline can then be sampled at intervals, and its samples can be used to position other GameObjects, or the vertices of a LineRenderer, or whatever. We use it for cables, hoses, ropes and wires. This system works marvelously with normal animations, but once constraints are involved - such as constraining the nozzle of a hose to a character's hand - there's no safe method in which to sample the Bezier Spline, and the resulting samples become a jittery mess.
Answer by silverbackAlex · Feb 08, 2019 at 03:38 PM
For any future internet archaeologists who stumble across the same problem; I spoke to some people at Unity, and this appears to be a bug. Sampling the position of a constrained object in LateUpdate should yield the correct position, but there is some kind of problem with the execution order.
The devs are talking a look at it now, so likely it will be resolved in the near future. I'd welcome any alternative solutions in the meantime, but as the central mystery has been solved, I'll answer my original question:
At the time of this answer, there isn't currently a good way (or more accurately, a good time) to sample the position of a constrained object. As part of the AnimationModule, constraints are meant to be applied before LateUpdate, but some kind of bug is preventing that. The talented folks at Unity are currently investigating the problem.
Thank you very much. I am currently doing some I$$anonymous$$ using IAnimationJobs, and have a similar problem - the constraints don't see to react to the changes in transforms that are manipulated at all. If this is being fixed, I guess I just have to wait?
Did you find any way to force an extra update/evaluation of the constraint system manager?
I'm told that update problem is fixed in "2019.2.0a11", so hopefully the change will be reflected in the "2019.2" release:
"Animation: Constraints are now evaluated before LateUpdate script (1123107)"
I didn't find a way to force an update, unfortunately. If you're not worried about making stable builds in the short term, you could try using the latest 2019.2 alpha, until the update.
Hope that helps!
Thank you so much for the information, I am that future, grateful internet archaeologist haha! Hope your problems with this are all better now :)
Your answer
Follow this Question
Related Questions
Parent and rotation constraints are influenced by sources whose weights are set to zero 2 Answers
Can I make animations snap to a frame? 1 Answer
How to select an animation clip by index number? 7 Answers
How to move hips in Unity Animation Rigging IK 0 Answers
How to play character animation after bone deparent? 2 Answers