Game Development Reference
In-Depth Information
gun.localToWorldMatrix *
rightHandMat);
handL.rotation = QuaternionFromMatrix(
gun.localToWorldMatrix *
leftHandMat);
}
Step 9. Play. The hands will now orientate with the gun. They don't
change position, just rotation, and therefore are a little inaccurate
when the gun is in some orientations. So far we haven't used any
inverse kinematic calculations. They start now. The objective will be to
rotate the shoulder, elbow, and wrist joint to suit the gun orientation.
It contains two C# code files from the Locomotion project for
calculating inverse kinematics. For more on this, see http://www.unity3d
.com/support/resources/unity-extensions/locomotion-ik.html . Unzip
these files. Create a folder in the Project called Plugins and place the
IK1JointAnalytic.cs and IKSolver.cs in it.
Step 11. Now modify animationController.js to use the inverse kinematics
code to rotate the shoulders, elbows, and wrists as shown in Listing 3.16 .
Listing 3.16 Implementing an Inverse Kinematic Joint
Rotation Solver
private var speed = 0.01;
...
var gun: Transform; //end effector
private var ikSolver:IK1JointAnalytic = new IK1JointAnalytic();
...
function LateUpdate()
{
//IK ----
rightHandMat = ChangeCoordinateSystems(handR, gun);
leftHandMat = ChangeCoordinateSystems(handL, gun);
gun.LookAt(aimPoint.transform.position);
gun.transform.Rotate(Vector3.up,90);
//Right arm
var desiredRightWristPosition: Vector3 =
(gun.localToWorldMatrix * rightHandMat).
MultiplyPoint3x4 (Vector3.zero);
var bonesR: Transform[] = new Transform[3];
bonesR[0] = armR;
bonesR[1] = forearmR;
bonesR[2] = handR;