- Home /
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.
https://answers.unity.com/questions/402259/how-to-combine-mecanim-and-script-based-bone-movem.html
https://answers.unity.com/questions/126368/how-to-rotate-a-bone-during-an-animation.html
Help is very much appreciated and I wish wonderful things upon you.
Answer by ishumshark · Jul 17, 2019 at 08:29 PM
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 to the animated eye objects is exactly what I was looking for! Awesome and 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:
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 rena$$anonymous$$g the animation path accordingly. See also the following post: https://answers.unity.com/questions/981781/animation-window-yellow-text-after-moving-animatio.html
Secondly, in my case the Animation Clip was embedded inside the .fbx model and was Read Only. This means you can assign a new path but it won't save to the model. If you close Unity, the animation 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 $$anonymous$$otion in the inspector.
Thanks again for the help!
Your answer
Follow this Question
Related Questions
Run smooth continuous rotation animation of spinner using animator 1 Answer
FIXED - Editing animation clips gives NaN on rotation value 2 Answers
In Editor add animation clips to animator controller via code 1 Answer
How to get reference to a specific Animation Clip in Animator. 0 Answers
Rotation problem while dynamically changing animations 2 Answers