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
}
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 _= bones*.localRotation;*_
boneDirections = bones*.localRotation;*
* }*
* }*
* // update angles after every input*
* void UpdateDirections() {*
* for(int ii = 0; i < bones.Length; i++) {*
boneDirections = bones*.localRotation;*
* }*
* }*
* // reset rig (this demonstrates the use of the ‘initialDirections’ array*
* void ResetPositions() {*
* for(int i = 0; i < bones.Length ; i++) {*
bones_.localRotation = initialDirections*;
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)