How to rotate a bone during an animation?

I have a character with idle and walk animations. Now I want to rotate the upper body without stopping the animations. So only one bone should stop playing the animation. If I try to stop the animation with animation.Stop(); it tells me that this bone isn’t playing any animations. The line of script that is supposed to be rotating the bone looks like this: transform.Rotate(0, Input.GetAxis(“Mouse X”) * sensitivityX, 0);. It doesn’t give me any console errors. Please help or ask for further information on my problem.

You have to rotate the bone every frame. You cannot stop the animation for a single bone (as long as I know); the animation will rotate the bone every frame to the animated value, so you have to rotate the bone every single frame (possibly in LateUpdate, so it happens after the animation updates your bone rotation).

If the bone you want to not animate is at the top of a chain (suppose the lower spine,) you could try to “subtract” it with:

// path in Heirarchy to bone. Set in Start():
Transform upperSpine = transform.find("bones/lowerSpine/upperspine");

animation["Walk"].AddMixingTransform(upperSpine);
// repeat for hips

AddMixingTransform (see the docs) is a wierd one. The first one subtracts all but that bone and below. The second+ is supposed to add more bone chains (but I haven’t done that.)

You upperSpine should then be completely non-animated, and “adding” method you are using should work.

A more robust solution is to just play the animation and overwrite the bone’s transform. The problem is, even if the bone never moves, the animation is resetting to that position ever frame. Adding 1 degree/frame won’t work any more – rotation is always reset to 0. Felix’s LateUpdate comment is correct – animation is set in Update, bones are positioned by animation after Update quits (overridding bone rots you made in update,) and then LateUpdate has a crack at them. Try:

// instead of changing rotation, change my rotation var:
float spineSpin = 0;
spineSpin += Input.GetAxis("Mouse X") * sensitivityX;
lowerSpine.eulerAngles = new Vector3(0, spineSpin, 0);

// complete guess. Math to apply overall model tilt to the spine:
lowerSpine.rotation = transform.rotation * lowerSpine.rotation;