- Home /
 
Bone movement by scripting using given angle as input.
I am currently working on a Unity project which uses a CyberGlove as input. It's bassically a glove with sensors which measures the angles between your fingers and joints. Now I want to simulate the movement in Unity by using a rigged hand so the hand on screen matches my real hand.
Could somebody provide me with a example on how to rotate bones with a given angle as input.
For example:
Angle: 45 degrees;
OnEvent() {
rotateJoint(angle, bone);
}
rotateJoint(angle, bone) {
// rotate bone 45 degrees
}
Answer by steviewondrs · Apr 09, 2013 at 08:28 PM
Allright, So I found the solution for rotating my skeleton joints with scripting First I mapped all the bones in public transforms, then I put them in an array, after which I can easily rotate them with EulerAngles.
     // mapping bones (drag and drop your prefered bones here)
     public Transform indexProximal;
     public Transform indexIntermediate;
     public Transform indexDistal;
     
     // array to store bones
     private Transform[] bones;
     
     // arrays to store the angles of the bones
     private Quaternion[] initialDirections; // for reset purposes
     private Quaternion[] boneDirections;
     private int ARRAYLENGTH = 3;
     
     void Start() {
         bones = new Transform[ARRAYLENGTH];
         initialDirections = new Quaternion[ARRAYLENGTH];
         boneDirections = new Quaternion[ARRAYLENGTH];
         
         // I map the bones into the array
         MapBones();
         // after which I store the initial directions
         GetInitialDirections();
     }
     
     // Map bones if they are available
     void MapBones() {
         if(indexProximal != null) { bones[0] = indexProximal;}
         if(indexIntermediate != null) { bones[1] = indexIntermediate;}
         if(indexDistal != null) { bones[2] = indexDistal;}
     }
     
     // get initial angles of bones in case the rig needs to be reset
     void GetInitialDirections() {
         for(int i = 0; ii < bones.Length ; i++) {
             initialDirections[i] = bones[i].localRotation;
             boneDirections[i]    = bones[i].localRotation;
         }
     }
     
     // update angles after every input
     void UpdateDirections() {
         for(int ii = 0; i < bones.Length; i++) {
             boneDirections[i] = bones[i].localRotation;
         }
     }
     
     // reset rig (this demonstrates the use of the 'initialDirections' array
     void ResetPositions() {
         for(int i = 0; i < bones.Length ; i++) {
             bones[i].localRotation = initialDirections[i];
             UpdateDirections();
         }
     }
     
     // Rotate joint to given angle
     void RotateJoint(int bone, float angle) {
         if(bones[bone] != null) {    
             Quaternion target = Quaternion.Euler(0, 0, angle); // define target angle, in this case a rotation around Z axis
             bones[bone].localRotation = target * boneDirections[bone]; // rotate bone to new position
             UpdateDirections(); // update the directions
         }
     }
 
               Note: this script isn't finnished! I just put it here to show how I solved the problem, read it through and understand the code. (Oh and if you got any suggestions to improve, please reply)
Hello sir. I am doing something similar but with the whole body (motion capture) I am using several I$$anonymous$$Us and get their rotations in Euler angles which I pass to unity. could we by any chance talk about how I should go about doing this? You seem to have worked this out an I am pretty new to unity so any help would be kindly appreciated.
@steviewondrs and @theophilos01 I also want to drive a full body via scripting. Have you guys made more progress in that regard?
Your answer