Combining rotation movement from C# script with rotation movement from animation

Dear unity friends,

The 3D model in my scene has a looping idle animation which among others rotates the eyes.
Through C# I also rotate the eyes towards the camera position:


public GameObject leftEye;
public GameObject rightEye;
public GameObject cam;

private void LateUpdate()
{
 // Find out where we want to look    
Quaternion targetRotation = Quaternion.LookRotation(cam.transform.position - leftEye.transform.position);
 targetRotation *= Quaternion.Euler(-90f, 0f, 0f);
 leftEye.transform.rotation = targetRotation;
 rightEye.transform.rotation = targetRotation;
 }

I’d like to add the eye rotation movements from the animation relative to the rotation movement from the script. At the moment the script overrides the animation movement.

The problem in the posts below are kind of similar but I haven’t managed to derive a solution to my problem from them or from other sources.

Help is very much appreciated and I wish wonderful things upon you.

To the best of my knowledge, it is impossible to simultaneously control the transforms of objects with an animation and from a script. However, it is possible to split that control between a parent and child.

I’m not sure what your animation is doing to the eyes, but I’m guessing you are using the script to point the eyes in the general direction of the camera, and then control the finer movements with the animation?

If so, I would child each eye object to an empty game object, then use your script above to point that object, with the eye inside, at the camera. Then you can use an animation to control the eye itself, so it would rotate and move relative to the empty parent.

@ishumshark “I’m not sure what your animation is doing to the eyes, but I’m guessing you are using the script to point the eyes in the general direction of the camera, and then control the finer movements with the animation?”
Yes that is exactly what I’m trying to achieve. And your solution of parenting an empty object with the script attached to the animated eye objects is exactly what I was looking for! Awesome thanks a lot!!

I did however run into some follow up problems which I’ll describe here with the solutions that worked for me, in case other people run into them:


  1. Firstly, when adding a parent game object to the hierarchy of your animated game object it breaks the animation path to all its child objects. In the Animation Window you’ll have to re-link the broken animation links, highlighted with yellow text, by clicking twice on the highlighted text (or pressing f2 when it’s selected) and renaming the animation path accordingly. [143191-unityforumpostimage02.jpg|143191]
    See also the following post:
    animation window, yellow text after moving Animation component - Questions & Answers - Unity Discussions

  1. Secondly, in my case the Animation Clip was embedded inside the .fbx model and was Read Only (see image) [143186-unityforumpostimage01.jpg|143186]
    This means you can assign a new path but it won’t save to the model. If you close Unity, the animations paths will be broken again next time you start it up. What worked for me here was to duplicate (Ctrl+D) the Animation Clip inside the model in the assets folder, better described by user “davejas” in the following post:
    https://forum.unity.com/threads/removing-read-only-from-animation-clips.514549/

In case you’re working with the Animator Controller to animate your object like I was, you’ll have to reassign the duplicated Animation Clip to the appropriate state. Go to the Animator Window press on the state you wish to assign the animation to and drag the new Animation Clip from your assets folder to Motion in the inspector.

Thanks again for the help!

P.S. It wouldn’t let me reply to answers since I don’t have 15 reputation or something…