Legacy animation state sampling and reverting

Hello there,

I’m using the legacy animation system with some procedural elements for a 2.5D shooter. I’m calculating the player’s torso aiming rotation based on the weapon’s current forward, but i need it to not be affected by animations like firing. I figured i’d achieve this by saving the specifics on all animation states in the animation component, disable them all save for the aiming one, sample the weapon’s current forward and then revert back to the way the states were. For some reason this does not work and at the end of the frame i’m left with just the aiming animation playing.

What am i doing wrong?

var animStates:List.<AnimationState>;
   function LateUpdate(){
       SampleStates();
       animationObject.animation["Aim"].enabled = true;
       animationObject.animation["Aim"].weight = 1;
       animationObject.animation.Sample();
       currentForward=weapon.transform.forward;
       RevertStates();
    }
    function SampleStates(){
    	for (var state : AnimationState in animationObject.animation){
    		animStates.Add(state);
    	}
    	for (state in animationObject.animation){
//iterate a second time just in case changing a state affects other states immediately
    		state.enabled=false;
    		state.weight=0;
    	}
    }
    function RevertStates(){
    	for(var state:AnimationState in animStates){
    		animationObject.animation[state.name].enabled=state.enabled;
    		animationObject.animation[state.name].weight=state.weight;
    		animationObject.animation[state.name].time=state.time;
    	}
            animStates.Clear();
    }

Well, AnimationState is a class and therefore a reference type. When you store them in array you don’t really save their state but a reference to the instances. AnimationState is actually derived from “TrackedReference” and the managed object itself doesn’t have any fields, only properties which call methods in native code. If you want to save the actual state you would have to copy all values in a custom class. Here’s an example in C# (I usually don’t use UnityScript and realised too late that you do ^^)

// C#
public class AnimStateSave
{
    public string name;
    public bool enabled;
    public float weight;
    public float time;
    public float speed;
    public int layer;
    public AnimationBlendMode blendMode;
    public AnimStateSave(){}
    public AnimStateSave(AnimationState state)
    {
        CopyFrom(state);
    }
    public void CopyFrom(AnimationState state)
    {
        name = state.name;
        enabled = state.enabled;
        weight = state.weight;
        time = state.time;
        speed = state.speed;
        layer = state.layer;
        blendMode = state.blendMode;
    }
    public void ApplyTo(AnimationState state)
    {
        // state.name = name;
        state.enabled = enabled;
        state.weight = weight;
        state.time = time;
        state.speed = speed;
        state.layer = layer;
        state.blendMode = blendMode;
    }
}

So instead of storing "AnimationState"s in your List you would store “AnimStateSave”

    animStates.Add(new AnimStateSave(state));

To assign them back you can simply do:

    state.ApplyTo( animationObject.animation[state.name] );