- Home /
Camera on rails: Inaccurate Animations
Hi,
In my application, I am using animations to move a camera around a scene back and forth between specific locations (essentially a camera on rails). The animations, however, do not appear to accurately move to the same precise locations each time (i.e. it doesn't always reach the exact end-point, thereby skewing the resulting view, etc.).
Does anyone happen to have any ideas why my animations may not be moving in a precise fashion? Is that expected behavior (meaning I really shouldn't be doing it this way)?
NOTE: The only thing managing my camera motion is the animations themselves and I am not blending them. They work individually and do not overlap.
Bill
NOTE: If further information about my setup would help to get to the bottom of this, please let me know and I'll fill in more details.
Answer by Paulius-Liekis · Dec 14, 2010 at 09:28 AM
There is a bug in Unity which causes animation not to be sampled at last frame. As a workaround set your animation wrap mode to ClampForever or control animation time yourself.
Answer by BillyK · Dec 14, 2010 at 10:06 PM
Thanks for the info. It was very helpful...
The problem with the "ClampForever" route is due to the fact that I'm leveraging the animations in reverse in order to return to the original point in the scene. When an animation is set to "ClampForever", it appears to elongate the length of the animation (essentially playing the last frame of the animation forever). When I reverse the animation, it then appears to pause for a while prior to triggering the move in reverse. The length of the pause is the length of time that the animation was running before the reverse was triggered (i.e. the enlogated portion of the animation). This same issue appears to be the case when the animation is run in reverse, regardless of the wrap mode. That was something I worked around a while back, actually.
Based upon your info, though, I did come up with a route that works. My original fix for the never-ending reverse issue (mentioned above) involved the use of animation events at the beginning and end of the animation. When those are triggered, the code will cause the animations to stop (when running in reverse). In order to fix the issue involved in this thread, I augmented the code in those events to force the animation to the appropriate time and force a sample at that point. The code looks like this:
public void AnimationStartReached(AnimationEvent animationEvent) { string animationName = animationEvent.animationState.name;
if (_isPlayingReverse)
{
AnimationState animationState = animationEvent.animationState;
animationState.time = 0f;
animation.Sample();
animation.Stop(animationName);
}
}
This forces the animation to hit its endpoint, regardless of the variable frame rate. From what I can tell, this appears to resolve my issue, forcing the animations to hit their precise end points.
Anyone see any issue with this approach?
Thanks again.
Bill
Yes, forcing animation sample at desired point is probably the approach that I would take too until the actual issue is fixed on Unity side.