Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mecanim Animation + SetLookAtPosition

Discussion in 'Animation' started by TRuoss, Sep 8, 2014.

  1. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    Is it possible to combine a hole Character animation with the SetLookAtPosition function from unity?

    If i use no default State the head follows my Target. If i drag and drop an action into mecanim the head does not follow my target anymore.

    Is there any special layer i need? or any kind of masking? Or is it not allowed to key the head?

    Thanks for any kind of advice.
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes you can do this.

    To achieve this you need to call your SetLookAtPosition in the right callback otherwise the animation system will overwrite what you have done.

    You have to use the MonoBehaviour.LateUpdate() callback, which is called right after all Update function has been called.
     
  3. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    I did what you wrote, but it´s not working ether.

    My Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimatedAvatar : MonoBehaviour
    5. {
    6.     public Transform viewTarget;
    7.     Animator anim;
    8.  
    9.     void Awake()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     void LateUpdate()
    15.     {
    16.         if (viewTarget)
    17.         {
    18.             anim.SetLookAtPosition(viewTarget.position);        
    19.            
    20.             anim.SetLookAtWeight(1.0f);
    21.         }
    22.     }
    23. }
    SetLookAtPosition.JPG
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
  5. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    does not work. Can i send you a demo project? It is about 50mb.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimatedAvatar : MonoBehaviour
    5. {
    6.     public Transform viewTarget;
    7.     Animator anim;
    8.  
    9.     void Awake()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     void OnAnimatorIK(int layerIndex)
    15.     {
    16.         if (viewTarget)
    17.         {
    18.             anim.SetLookAtPosition(viewTarget.position);        
    19.          
    20.             anim.SetLookAtWeight(1.0f);
    21.         }
    22.     }
    23. }
    Project Link: https://drive.google.com/file/d/0B8nAeEN_arSfZWxZTXVjRWJQS3c/edit?usp=sharing

    - load scene SetLookAtPosition and press play
     
    Last edited: Sep 9, 2014
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I couldn't load your project, I don't have blender installed.

    So I did a small project in Unity 4.3 since I didn't know which version you have.
    Open LookAt scene, press play, and move the weight slider to activate the look at.
    The character is animated with a Idle clip.

    https://www.dropbox.com/s/aphpudl475yz4f2/LookAtTest.unitypackage?dl=0


    And by the way do you have Unity Pro? because IK is only available to Pro user.
    I did assume you had it because in a previous post you said
    Best regards,
     
  7. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    Thanks! I tested my setup and the problem must be in my keyframe animation i have done in blender. everything works unitl i put in any kind of keyframe animations i did.

    Are there some bones i´m not allowed to set keys to?
     
  8. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    [SOLVED]

    it was so stupid. i can´t stop crying how much time and money i waste on this.

    Situation: I have two blend files. One has the final rigged model i use in my scene. the other one i´m saving my actions into. The Problem was that both have to be imported as humanoid! I only changed the model i use. But unity imported the animation from the second file differently so it did not work.

    Short Checklist on this:
    - check everything is imported as humanoid
    - check if you enabled IK Pass in your Controller
    - check that you set animator.SetLookAtPosition(...) and animator.SetLookAtWeight(...)
     
  9. HamFar

    HamFar

    Joined:
    Nov 16, 2014
    Posts:
    89
    Do we necessarily need to have an Animator Controller, in order to be able to use the SetLookAtPosition method? I tried using SetLookAtPosition and OnAnimatorIK in a simple script that references an animator, but apparently without an actual Animator Controller (that contains at least an idle animation), IK functions do not run. Sadly, I cannot have an Animator Controller because it makes my motion capture scripts useless, for some reason... Is there any way around this?
     
    david-mal likes this.
  10. Kirzo

    Kirzo

    Joined:
    May 10, 2017
    Posts:
    1
    For those who doesn't have Unity Pro, you can try this:
    Code (CSharp):
    1. private void OnAnimatorIK (int layerIndex) {
    2.      Transform head = animator.GetBoneTransform(HumanBodyBones.Head);
    3.      Vector3 forward = (lookAtPosition - head.position).normalized;
    4.      Vector3 up = Vector3.Cross(forward, transform.right);
    5.      Quaternion rotation = Quaternion.Inverse(transform.rotation) * Quaternion.LookRotation(forward, up);
    6.      animator.SetBoneLocalRotation(HumanBodyBones.Head, rotation);
    7. }
     
    polous, C_R_M, dyupa and 1 other person like this.
  11. polous

    polous

    Joined:
    Mar 27, 2018
    Posts:
    4
    How can I add to this script weight?
     
  12. david-mal

    david-mal

    Joined:
    Nov 27, 2017
    Posts:
    6

    Is there any solution for this? Its not about the LookAtPosition thing, but about IK ingenerall. Somthing like... I get the tracking data, its mapped on HumanPose. and AFTER that I want to set the look at target.
     
  13. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    A bit late but Vectore3 doesn't support "-" command. Do you have a workaround for this maybe?
     
  14. Kondrya

    Kondrya

    Joined:
    Nov 25, 2016
    Posts:
    1
    If you nead a weight :
    Code (CSharp):
    1. animator.SetBoneLocalRotation(HumanBodyBones.Head, Quaternion.Lerp(head.localRotation, rotation, weight));