Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Using muscles to move fingers

Discussion in 'Scripting' started by AntiProfessional, Feb 28, 2018.

  1. AntiProfessional

    AntiProfessional

    Joined:
    Feb 28, 2018
    Posts:
    4
    Hey community,
    I am currently trying my luck with Unity and I kinda like it.
    I've created an humanoid model with a rig and an animation attached by myself with blender.
    I've imported my model to a Unity sceen and it works to move and look around (Controlled by the Standard FirstPersonController). Also my Animation (which basically just raises the hands) works without problems.
    Now i want to move fingers when the user presses some keys on the Keyboard (e.q. close the index finger when pressing 'F' on the keyboard). I tried that by using the HumanPoseHandler with a humanPose, but it's not working.
    Code (CSharp):
    1.         private Animator m_Animator;
    2.         private bool m_ShowHands = false;
    3.         private bool m_UnshowHands = false;
    4.  
    5.         private bool m_MoveFingers = false;
    6.  
    7.         private HumanPose humanPose;
    8.         private HumanPoseHandler humanPoseHandler;
    Code (CSharp):
    1.         // Use this for initialization
    2.         private void Start()
    3.         {
    4.             m_Animator = GetComponentInChildren<Animator>();
    5.             humanPose = new HumanPose();
    6.             humanPoseHandler = new HumanPoseHandler(m_Animator.avatar, transform);
    7.             humanPoseHandler.GetHumanPose(ref humanPose);
    8. // some other stuff follows here from the FirstPersonController
    9. }
    In FixedUpdate I get the userinput, which works fine.
    Now, I heard that my animations might get overwritten from my animationcontroller, thats why I've put my script in 'LateUpdate', which should move my index finger
    Code (CSharp):
    1.         private void LateUpdate()
    2.         {
    3.             if (m_MoveFingers)
    4.             {
    5.                Debug.Log("manipulate indexfinger-right");
    6.                 humanPoseHandler.GetHumanPose(ref humanPose);
    7.  
    8.                
    9.                 humanPose.muscles[79] = -40f;
    10.                 humanPose.muscles[80] = -40f;
    11.                 humanPose.muscles[81] = -40f;
    12.                 humanPose.muscles[82] = -40f;
    13.  
    14.                 humanPoseHandler.SetHumanPose(ref humanPose);
    15.             }
    16.         }
    But when I set m_MoveFingers to true, nothing happens. The Debug.Log is called, but the model won't move. I removed the Controller from the Animator, but it still doesn't move.
    My AnimatorController attached to the model is the one which comes with the ThirdPersonController Standard Asset.

    I am kinda lost right now and not sure how I can fix this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    I played around with something similar to this a few years back. It was tricky to get animation bones to "drive" the model manually, the way you are doing here in code.

    If I recall, a couple of critical steps were necessary, all done at once in code:

    - the animation had to actually be playing (just call .Play() on it)
    - it obviously had to be enabled as well
    - then you drive the bones to the new positions you want (I assume this is what your pose handler does from the muscles?)
    - then you call .Sample() on the animation, which skins the bones out to the mesh.
    - and finally you stop the animation so it doesn't actually move it further at the end of frame.

    This is the reference on the .Sample() function:

    https://docs.unity3d.com/ScriptReference/Animation.Sample.html

    If I recall, that script is not enough, inasmuch as it assumes the animation is already playing. If it isn't playing, calling .Sample() doesn't do you any good.
     
  3. AntiProfessional

    AntiProfessional

    Joined:
    Feb 28, 2018
    Posts:
    4
    Hello Kurt-Dekker,
    Thank you for your answer. But I don't think this will fix my problem. I have not created an animation for moving the fingers. I just wanted to move the finger by manipulating the humanPose via the humanpose handler (see the last code snippet).
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Ah, missed that detail. I've not played with the HumanPose stuff myself. Maybe someone else here can chime in? Does it work like a traditional skinner? Or is it more like the mechanim bone indirector? Not sure really. Good luck!