- Home /
Mecanim/Animator SetLookAtPosition not working
I have a character controlled by a Mecanim Animator. I want to be able to precisely position the character's head.
public Transform lookTransform; // An object set in the editor, not null
// Update is called once per frame
void Update () {
if (lookTransform != null) {
Animator animator = GetComponent<Animator>();
animator.SetLookAtPosition(lookTransform.position);
animator.SetLookAtWeight(1.0f);
}
}
This results in no effect whatsoever. As far as I can tell, I'm using the same code from the Mecanim example project (the one where the robot shoots lasers at the floating sphere). I'm using Unity Free; is that possibly the reason?
also a very common gotchya: http://answers.unity3d.com/answers/650502/view.html
Answer by drizztmainsword · Feb 18, 2013 at 12:52 AM
I apparently have a nasty habit of answering my own questions.
It would appear by looking at the Unity Store that the head look functionality is limited to the Pro version of Unity. This is the only place I have found any information about it; there is no mention of the feature being Pro only in the scripting documentation. There is also no warning within Unity when attempting to call this Pro-only feature; it merely fails silently.
It looks like I'm going to be building my own.
There already exists such a script http://wiki.unity3d.com/index.php?title=HeadLookController . It works great with the old Locomotion-System but I didn't test it with $$anonymous$$ecanim.
For others browsing this question and using Pro, you should call SetLookAtPosition() and SetLookAtWeight() inside of OnAnimatorI$$anonymous$$(), not in Update().
@ waschtel: Doing it myself has actually been rather beneficial. It has allowed me to have head oriented based on the root or the chest of the character, it has allowed me to control whether or not the chest is included in the I$$anonymous$$ algorithm, and it works directly on mouse/joystick movements ins$$anonymous$$d of by telling the head to look at a point in space.
@TonyLi: I would imagine OnAnimatorI$$anonymous$$() runs at some point around LateUpdate(), which is where you would go to modify any animations. In fact, I never checked using those functions in OnAnimatorI$$anonymous$$(), so maybe they will work there? No matter, I have a version more suited to my needs now.
Thank you for answering this yourself. I thought I was doing something wrong for the past hour. Even re-imported and re-setup the avatar b/c I thought I might have set that part up wrong.
Oh nice! Thanks for pointing that out, I have been wondering that for the past few hours too. Unity should really notify that in the console or something.
Answer by Stardog · May 31, 2015 at 11:35 PM
OnAnimatorIK will only work if it's on the same GameObject as the Animator component.
Answer by peter.parker · Oct 13, 2014 at 11:59 AM
First, write this in Start() of any script for Pro version
bool hasPro = UnityEditorInternal.InternalEditorUtility.HasPro();
print ("Unity Version Pro: " + hasPro);
If it returns true, then reconfirm by running this
void OnAnimatorIK (int layerIndex) {
print ("OnAnimatorIK - running");
}
As, the above code only runs in Pro version.
Remember that OnAnimatorIK is a callback function, so it must be on the object having animator component. Also, remember that in your animator controller, in your animation layers IK Pass should be checked.
Hope, it will save someone's time finding problem on IK.
And look here:
Your answer
Follow this Question
Related Questions
Mecanim state that keeps last pose? 0 Answers
Switching Animator on/off 0 Answers
Mecanim CrossFade transition interruption issue 3 Answers
Clone character with new character's animator playing at the same transition position 0 Answers
Should I use playables, mecanim, or legacy to create keys and clips ? 0 Answers