Two animations frozen at different positions?

I have already scripted a procedural animation system which calculates two independent values for the amount a snow boarder is crouched and the amount they are twisted. These values change every frame depending on user input and the forces acting on the boarder.

I can crudely pose the character using these values by calculating the transforms of bones inside the a model. However I would like to move the system over to one based on two frozen animations - one for each control parameter (crouch and twist) The idea is that the character would be posed by selecting one moment from each of the two simultaneously applied animations.

But I can’t find anyway to do it.

I have import the model complete with armature and ‘Twist’ and ‘Crouch’ animation clips into unity as an .fbx. I know this has worked because I can play with the actions separately in the properties tab

I created a prefab from the boarder.

I have created an Animation Controller asset and populated it with states corresponding to the Twist and Crouch animations from my imported border (fbx) by dragging and dropping the clips into the state machine

I added references to the ‘Twist’ and ‘Crouch’ clips to the animation module of the boarder prefab.

I have added an Animator component to the boarder prefab;

I have set the ‘Controller’ property of the Animator component to refer to Animation Controller I have created.

I then get a reference to the Animation component and try to reference the clips as follows but the references are null;

Animation anim = gameobjectBoarder.animation;
AnimationState animstateTwist = anim[“Twist”];

animstateTwist ends up null so I can not take the next step which would be…

animstateTwist.time = 0.5f;

does anyone know why this is not working?

I am working in unity 4.3

Any help would be greatly appreciated.

OK I have got it working and I was not too far away. For anyone else struggling with the same thing. Here is what I have found out.

Exporting animations from blender as an fbx file.

  • You have to create an armature then put the bones in it then attach the various bits to the bones.
    -To do your animations you have to select the armature in object mode then go into Pose Mode which only appears in the edit mode drop down when you have an armature selected. Only animations created in this way will be exported with the fbx.
  • You can create more than one animation for your armature using the ‘Dope Sheet’ panel in ‘Action Editor’ mode. There is a plus which you click to duplicate the current ‘action’ (animation) you have to mess around with the F key button as well its very finicky and frustrating. Note when adding actions you have to be in Pose Mode for the armature you are editing. I got in a real state having the wrong thing selected when adding animations you have to watch this really carefully.

With this done you can export to an Fbx file in your assets folder. Then go to unity. Here is what I have found out about preparing your animatable model asset.

  • With the asset selected you should see 3 buttons at the top of the inspector panel. click the middle one (‘Rig’) and select ‘legacy’. I am not sure what the exact consequences of this are (I think it means you can’t use the avatar or state machine system) but you have to do it if you want your clips to be available through the animation component - which is what you want.
  • Click the animations button at the top and you can see the animations panel… You now have to tell unity which of the actions you want to use. You do this by selecting an action you want in the drop down labeled ‘Source Take’ then clicking the plus button just above that. You should see the name of the selected action added to the list. Do this for the actions that you want to use in your game. The actions are now referred to as clips as they are unity constructs rather than fbx ones.

Now you are ready to create your prefab.

Drag the asset into the scene and then drag the resultant gameobject back into the project tree (asset tree) hopefully you already know how to create prefabs.

The prefab will already have an animation module but again you will have to tell unity which the actions you want the animation component to have access to. You do this in the animation component inspector by entering the number of clips into the ‘Size’ text box and hitting return. A list of reference selectors will then appear - one for each clip. Click the little dohicky at the right of each slot to select a clip for that slot.

You are now ready to pose the model from your script. Here is what I have found about posing the model.

Just once you have to setup the clips as follows

public Animation m_animation;
public AnimationState m_animstateTwist;	
public AnimationState m_animstateCrouch;

m_animation = gamebbjectBorder.animation;
m_animstateTwist = m_animation["Twist"];
m_animstateTwist.enabled = true;
m_animstateTwist.weight = 1f;
m_animstateTwist.speed = 0; //to make the animation pause
m_animstateCrouch = m_animation["Crouch"];
m_animstateCrouch.enabled = true;
m_animstateCrouch.weight = 1f;
m_animstateCrouch.speed = 0; //to make the animation pause			

Then every frame you can pose the character by applying a single position from the two clips/animations/actions as follows

m_animstateTwist.normalizedTime = WorkOutTheTwistPositioin();    			
m_animstateCrouch.normalizedTime = WorkoutTheCrouchPosition();

Notice I have used the normalizedTime property . This takes a value between 0 (the start of your animation) and 1(the end of the animation) so you don’t have to worry about frames or playback rates etc

I hope this saves someone from days of frustration. :slight_smile: