Using StateMachineBehaviours for procedural animation

I tried writing a state machine behaviour for procedural animation. As a simple test, within OnStateUpdate(), I grabbed a bone (such as the head bone) and set its rotation to a random rotation.

The expected behaviour is that the characters head would twitch out, but it doesn’t seem to move at all.
There is only one state and one layer in the animator.

I’ve also tried using OnStateMove(), but this also didn’t work.

Is there something I’m doing wrong, or is this not an intended usage?

Try OnStateIK().

I’ve just tested it - unfortunately that doesn’t work either.

Is the head under IK control? According to the docs, the call order is:

  • MonoBehaviour.OnAnimatorIK()
  • StateMachineBehaviour.OnStateIK()
  • Mecanim’s internal IK

So if Mecanim applies any IK to the head in step #3, it’ll overwrite any work you did in #2.

I don’t think it is. At least not explicitly. Technically, IK isn’t applied to the head - it can apply look rotation though, which would affect this step… But I didn’t enable it.

I’ll try affecting a different bone, like… the arm or the jawbone.

I made it so that in OnStateIK(), EVERY bone is randomly rotated. No motion whatsoever.

override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
for (int i = 0; i < (int)HumanBodyBones.LastBone; i++)
{
var bone = animator.GetBoneTransform((HumanBodyBones)i);

if(bone != null)
bone.rotation = Random.rotation;
}
}

Are you sure the controller is in this state? If you add some Debug.Log lines, do they write to the console? Does your method signature exactly match the one below?

void OnStateIK(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)