How to rotate spine by the script, while an animation from the animator is playing?

Hi! I want to create third person shooter, but I have got one problem. I want to rotate shooter’s spine by script, during an animation from the animator. And while the animation is playing, I can’t rotate spine by the script. I don’t know how can I do it. I’ll be very grateful if someone helps me.

When you begin using Mecanim for Animations you must play by its rules. Mecanim updates transforms later then Update and FixedUpdate messages, so modifying transforms in there that are now under the control of the Animator will just be overridden. So in order to tell Mecanim we want to look at a certain point or procedurally change the rotations of bones we must use Inverse Kinematics (IK). The first step to using IK in Unity is to enable an ‘IK Pass’ on your Animation layer the documentation here shows you where the tick box is to enable that.

Once you have done that the Animation system will now perform Inverse Kinematics calculations and provides you with a call back to use in your scripts with the MonoBehaviour.OnAnimatorIK message. You may be able to simply use the inbuilt LookAt functionality to get what you are looking for. If so it could be as simple as the following to get what you are looking for:

using UnityEngine;

public class LookAt : MonoBehaviour
{

    public Transform lookTarget;
    public float lookWeight;
    public float bodyWeight;
    public float headWeight;
    public float eyesWeight;
    public float clampWeight;

    Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    private void OnAnimatorIK(int layerIndex)
    {
        // Set our Look Weights for this pass
        animator.SetLookAtWeight(lookWeight, bodyWeight, headWeight, eyesWeight, clampWeight);
 //Now set our position to look at for this pass
        animator.SetLookAtPosition(lookTarget.position);

    }
}

Where lookTarget is the GameObject your looking at (The Editor will still allow you to drag and drop GameObjects into a Transform type variable, unless you need the actual game object for a reason why not save a variable). You can find out what the parameters mean by looking at the documentation Animator.SetLookAtWeight and Animator.SetLookAtPosition

However if this does not work for you, you can also look at other possibilities such as Animator.SetBoneLocalRotation (The documentation actually suggests that it can be used in a similar situation to what you are looking for).

A nuclear option does exist if you do not wish to use IK, you can use the LateUpdate message in your script to override the animation systems transforms for bones, but that is not the best practice I find.

Hopefully this will get you on the right track to getting that character of yours pointing in the right direction! Best of Luck!