Unity5: Move Humanoid's Bones Directly with C# Program


2017.08.02: created by
2017.08.03: revised by
2017.08.04: revised by
Japanese English
To Table of Contents

Rotate Humanoid's Bone in each local coordinate system directly with C#

Runtime Video Humanoid.mp4



The Coordinate System in Unity

Unity's coordinate system is left-handed. In the left-handed coordinate system, the positive rotate direction about each axis is "clockwise when looking at the origin from infinity of its axis".

Positive Rotate Direction Around Each Axis

Bones of Humaonid of Unity

The skeleton of Unity's Humanoid is composed of layered bones. In "Mapping", solid outer circle and dotted outer circle indicates "Mandatory Bone" and "Optional Bone", respectively. The inner filled circle means Bone assigned in this Humanoid.



The type expressing Humanoid's Bone is Unity's HumanBodyBones , and the main ones are shown in the table below. In the table, the Bone which is the parent of each Bone's Transfrom was specified as "Parent Bone". In addition, we examined the orientation of the local coordinate axes in the Transform of each Bone, so the figure below shows it.

Bone NameMandatoryParent Bone
Hipso-
SpineoHips
ChestSpine
UpperChestChest
NeckUpperChest
HeadoNeck
LeftShoulderUpperChest
LeftUpperArmoLeftShoulder
LeftLowerArmoLeftUpperArm
LeftHandoLeftLowerArm
LeftUpperLegoHips
LeftLowerLegoLeftUpperLeg
LeftFootoLeftLowerLeg
LeftToesLeftFoot
RightShoulderUpperChest
RightUpperArmoRightShoulder
RightLowerArmoRightUpperArm
RightHandoRightLowerArm
RightUpperLegoHips
RightLowerLegoRightUpperLeg
RightFootoRightLowerLeg
RightToesRightFoot




Move the Skeleton of Unity's Humanoid directly with C#

I will explain how to move Unity's Humanoid as you like by C# program.

  1. Create new project in Unity.
  2. We create new "3D" project named "Humanoid".




  3. Import Humanoid model (fbx) into the Assets.
    1. Create a "Models" folder in the Assets.
    2. Assets -> Create -> Folder > Rename to "Models"
      
    3. Drag and import the "AsianBoy.fbx" and "textures/" into the "Assets/Models/" folder in the project window. They are created in "MakeHuman: Create a Humanoid Model to use in Unity5".



    4. [Notice] The import operation above can be also done by

      Assets -> Import New Asset... -> AsianBody.fbx
      . But in this way, the textures required for "AsianBoy.fbx" will not be imported automatically and the model may be completely white. In this case, you hove to import the textures corresponding to the white material generated in "Assets/Models/Materials/" manually.




    5. Select "AsianBoy" in the project window, change "Animation Type" to "Humanoid" in the "Rig" tab in the Inspector's window, leave Avatar Definition as "Create From This Model", and click the "Apply" button.



    6. If the skeleton is correctly associated as a Humanoid, a check mark appears before "Configure" . It is safer to click "Configure" in the Inspector window and make sure that the bone is set correctly.



    7. Click "Mapping" in the Inspector window to see the correspondence of bones. When you can confirm, click "Done".






  4. Place "Assets/Models/AsianBoy" in the Hierarchy.
    1. Drag "AsianBoy" of "Assets/Models" in the project window into the Hierarchy window.



    2. Select Reset from Transform's "Settings" in the Inspector window to set Transform Position (x,y,z)=(0,0,0).
  5. Create C# Scripts to operate Humanoid.
    1. Create a folder "Scripts" in the Assets.
      Right click in the project's Assets  -> Create -> Folder -> Rename it as "Scripts"






    2. Create a C# Script named "RigBone". Change the contents of the file as follows.
    3. Right click in the project's "Assets/Scripts" -> Create -> C# Script -> Rename it as "RigBone".
      
      Notice that "RigBone" class does not extend "MonoBehavior" class.





      Imported Humanoid data has an Animator component attached. In the Animator component, Bones are layered so as to be suitable for the human skeleton, and the Transform can be obtained with the GetBoneTransform(HumanBodyBones) function.

      The "RigBone" class provides a method for changing the localRotation of Transform with the specified quaternion q. Instead of giving a direct quaternion, we prepared same name function which are given rotation angle and rotation axis.

      RigBone.cs
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class RigBone {
        public GameObject gameObject;
        public HumanBodyBones bone;
        public bool isValid;
        Animator animator;
        Quaternion savedValue;
        public RigBone(GameObject g, HumanBodyBones b) {
          gameObject = g;
          bone = b;
          isValid = false;
          animator = gameObject.GetComponent<Animator>();
          if (animator == null) {
            Debug.Log("no Animator Component");
            return;
          }
          Avatar avatar = animator.avatar;
          if (avatar == null || !avatar.isHuman || !avatar.isValid) {
            Debug.Log("Avatar is not Humanoid or it is not valid");
            return;
          }
          isValid = true;
          savedValue = animator.GetBoneTransform(bone).localRotation;
        }
        public void set(float a, float x, float y, float z) {
          set(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
        }
        public void set(Quaternion q) {
          animator.GetBoneTransform(bone).localRotation = q;
          savedValue = q;
        }
        public void mul(float a, float x, float y, float z) {
          mul(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
        }
        public void mul(Quaternion q) {
          Transform tr = animator.GetBoneTransform(bone);
          tr.localRotation = q * tr.localRotation;
        }
        public void offset(float a, float x, float y, float z) {
          offset(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
        }
        public void offset(Quaternion q) {
          animator.GetBoneTransform(bone).localRotation = q * savedValue;
        }
        public void changeBone(HumanBodyBones b) {
          bone = b;
          savedValue = animator.GetBoneTransform(bone).localRotation;
        }
      }
      
      
    4. Create a C# Script "RigControl" in "Assets/Scripts".
    5. Right click in the Assets/Scripts -> Create -> C# Script -> rename it as "RigControl"
      






      Humanoid operates at 2-second cycles. It Shakes the left upper arm and the left lower arm in the horizontal direction and the right upper arm in the vertical direction. It repeated bending and streching the right sigh and right knee by 90 degrees.

      Each bone is set the next range of rotation in its local coordinate system.

      BoneAxisMinMax
      LeftUpperArmX-axis-8080
      LeftLowerArmX-axis090
      RigtUpperArmZ-axis-9090
      RigthUpperLegX-axis90180
      RightLowerLegX-axis090

      In the following program, joints are bent using the RigBone.set(Quaternion) function. To rotate the entire Humanoid, set humanoid.transform.rotation to the value in the world coordinate system. The order of Rotation in Transform in Unity Editor is Y, X, Z in order, so apply here in that order as well.

      RigControl.cs
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using System.Linq;
      using System;
      
      public class RigControl : MonoBehaviour {
        public GameObject humanoid;
        public Vector3 bodyRotation = new Vector3(0,0,0);
        RigBone leftUpperArm;
        RigBone leftLowerArm;
        RigBone rightUpperArm;
        RigBone rightUpperLeg;
        RigBone rightLowerLeg;
        void Start () {
          leftUpperArm = new RigBone(humanoid, HumanBodyBones.LeftUpperArm);
          leftLowerArm = new RigBone(humanoid, HumanBodyBones.LeftLowerArm);
          rightUpperArm = new RigBone(humanoid, HumanBodyBones.RightUpperArm);
          rightUpperLeg = new RigBone(humanoid, HumanBodyBones.RightUpperLeg);
          rightLowerLeg = new RigBone(humanoid, HumanBodyBones.RightLowerLeg);
        }
        void Update () {
          double t = Math.Sin(Time.time * Math.PI); // [-1, 1]
          double s = (t+1)/2;                       // [0, 1]
          double u = 1-s/2;                         // [0.5, 1]
          leftUpperArm.set((float)(80*t),1,0,0);
          leftLowerArm.set((float)(90*s),1,0,0);
          rightUpperArm.set((float)(90*t),0,0,1);
          rightUpperLeg.set((float)(180*u),1,0,0);
          rightLowerLeg.set((float)(90*s),1,0,0);
          humanoid.transform.rotation 
            = Quaternion.AngleAxis(bodyRotation.z,new Vector3(0,0,1))
            * Quaternion.AngleAxis(bodyRotation.x,new Vector3(1,0,0))
            * Quaternion.AngleAxis(bodyRotation.y,new Vector3(0,1,0));
        }
      }
      
      
  6. Create Empty Object in Herarchy and change the name to "Rigcontroller". Select Reset from the Transfrom Settings in the Inspector window to set Transform Position (x,y,z)=(0,0,0).






  7. Drag "Assets/Scripts/RigControl" in the Project window onto the "RigController" in the Hierarchy window, and drop it with the RigController surrounded by a blue ellipse.



  8. When you select Hierarchy's "RigController", the "Rig Control (Script)" component appears in the Inspector window, so you can see that it was added. Set the Humanoid in the Hierarchy ("AsianBoy" in this example) to the "Humanoid" of the component.



  9. Change the position of Main Camera in Hierarchy.
  10. Because the Main Camera is a little too far away, lets set its Transform Position (x,y,z) = (0,1,-2).




  11. Save a scene as "Assets/Scenes/rig.cene"
  12. File -> Save Scene As ... 
    
  13. Click to execute this program.



  14. When executing the program, some bones moves in 2 second cycle.

    While executing the program, select "RigController" in the Hierarchy window and set the "BodyRotation" of the "Rig Control 2 (Script)" component in the RigController Inspector window to (X,Y,Z)=(0,180,0), Humanoid will turn to the camera.




    Runtime Video HumanoidRig.mp4



Use initial value of localRotation (modified version above)

  1. Create new scene.
  2.   File -> Save Scene as .. -> rig2.scene
    
  3. Create C# Script at "Assets/Scripts/" and rename it as "RigControl2.cs"
  4. In "RigControl2.cs", we changed to use the RigBone class's offset(Quaternion) method, that is, it adds rotation to the initial value of localRotation of each bone.

    In "RigControl.cs" of the previous example, we use the set() function of the RigBone class. That is, since we moved the bone without using the initial value of localRotation of each bone, we needed to set a special value for rotation angle for each bone. For example, when RightUpperLeg is set to 0 degree, the foot points directly upward, so we set a valuebetween 90 degree and 180 degree.

    In "RigControl2.cs", since we add rotation to the initial value by using the offset() function of the RigBone class, if we give values around 0 degree to any bone, it works correctly. Of course, it is necessary to judge which one of the positive direction and the negative direction is a natural movement for the joint of each bone, but I think this is much easier way to think.

    Each bone is added the next range of rotation to the intial localRotation.

    BoneAxisMinValue
    LeftUpperArmX-8080
    LeftLowerArmX090
    RigtUpperArmZ-9090
    RigthUpperLegX-900
    RightLowerLegX090
    RigControl2.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Linq;
    using System;
    
    public class RigControl2 : MonoBehaviour {
      public GameObject humanoid;
      public Vector3 bodyRotation = new Vector3(0,0,0);
      RigBone leftUpperArm;
      RigBone leftLowerArm;
      RigBone rightUpperArm;
      RigBone rightUpperLeg;
      RigBone rightLowerLeg;
      void Start () {
        leftUpperArm = new RigBone(humanoid, HumanBodyBones.LeftUpperArm);
        leftLowerArm = new RigBone(humanoid, HumanBodyBones.LeftLowerArm);
        rightUpperArm = new RigBone(humanoid, HumanBodyBones.RightUpperArm);
        rightUpperLeg = new RigBone(humanoid, HumanBodyBones.RightUpperLeg);
        rightLowerLeg = new RigBone(humanoid, HumanBodyBones.RightLowerLeg);
      }
      void Update () {
        double t = Math.Sin(Time.time * Math.PI); // [-1, 1]
        double s = (t+1)/2;                       // [0, 1]
        leftUpperArm.offset((float)(80*t),1,0,0);
        leftLowerArm.offset((float)(90*s),1,0,0);
        rightUpperArm.offset((float)(90*t),0,0,1);
        rightUpperLeg.offset((float)(-90*s),1,0,0);
        rightLowerLeg.offset((float)(90*s),1,0,0);
        humanoid.transform.rotation 
          = Quaternion.AngleAxis(bodyRotation.z,new Vector3(0,0,1))
          * Quaternion.AngleAxis(bodyRotation.x,new Vector3(1,0,0))
          * Quaternion.AngleAxis(bodyRotation.y,new Vector3(0,1,0));
      }
    }
    
    
  5. Drop "Assets/Scripts/RigControl.cs" in Assets onto the "RigController" in Hierarchy to add " Rig Control 2 (Script)" component.
  6. Select RigController in Hierarchy, Set "AsianBoy" in Hierarcy to the "Humanoid" of "Rig Control 2 (Script)" component in the Inspector window.
  7. Since we do not use the "Rig Control (Script)" previously attached to RigController, either uncheck it so that it does not work or Remove Component.
  8. Execute the program.
  9. Since we added a rotation to the initial value of the localRotation of each Bone, it seems to be slightly distorted rotation. However, it seems that there is much better way to move Humanoid's bone by program.




    Runtime Video HumanoidRig2.mp4



The Unity's project is here (Humanoid.zip)


Yoshihisa Nitta

http://nw.tsuda.ac.jp/