Background
I am making a stairs in platformer game and for each step there is two animations. Step and Idle. Step animation moves skeleton's hip bone one step up. The Idle animation is the same as the last frame of step animation, but hip is reset to default position.
Here is step animation (two keyframes with hip's movement, no loop):
Loading Image
Loading Image
Here is idle one-frame-animation (one frame, hip in default position, loop)
Loading Image
I also moved a hand up in idle animation, to easy visually differ between this two animations.
This should work as following:
1) Play step animation
2) Move unity game object up and set animation to idle at the same time.
Hence idle animation has hip in default position, this gives a same world position as the last step frame.
Here is code that does that:
1)
if (character.Input.HorizontalAxisDigital == 1) {
my_skeleton.state.SetAnimation(0, "Step", false);
}
2)
my_skeleton.state.End += entry => {
if (entry.animation.Name.Equals(current_step_anim)) {
var stairs = ((Stairs)character.StoodOnPlatform);
character.Translate(stairs.StepDirection * currentStairs.StepSize.x, currentStairs.StepSize.y, true);
var anim_name = "Idle";
TrackEntry track_entry = ReferenceLvichka.skeleton_top_right.state.SetAnimation(0, anim_name, true);
UpdatePositionOnStairs();
}
The Problem
I have a one frame lag, which causes very unpleasant flicker. I will show this problem on these 4 frames:
1 (begin of animation "Step")
Loading Image
2 (end of animation "Step")
Loading Image
3 (player moved to next step and animation in inspector and Debug.Log is "Idle", but it looks like "Step" for one frame, flickering)
Loading Image
4 ("Idle" animation plays in loop as expected)
Loading Image
At the frame 3 animation is set to "Idle" and unity game object is moved to the next step. But on frame 3 game object is moved to the next step, but animation is still "Step", not "Idle". Logging TrackEntry or AnimationState to console shows that animation is "Idle". But you can see that hand is not over, so it is actually "Step".
What debug logging shows
At the frame 4 animation actually changes to "Idle" and it looks right. So the problem is flicker and one-frame lag. I have tried to log
FRAME_SINCE_START
STEP_NUMBER - ANIMATION_NAME - WORLD POSITION
Log:
Loading Image
What I have tried
1) Setting Default mix on 0 (from 0.2) does not seem to help.
2) Move this code in LateUpdate, may be it some execution order issues - does not seem to work.
3) Splitting changing game_object's position and setting animation with time. I have tried it with timer, it flicker anyway. May be I should wait exactly 1 frame (it might work, not tried it yet).
So I am not really sure why does it happen and would be grateful for any help.