Game Development Reference
In-Depth Information
{
}
function QuaternionFromMatrix(m: Matrix4x4):Quaternion
{
var q: Quaternion;
q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0,0] + m[1,1] +
m[2,2] ) ) / 2;
q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0,0] - m[1,1] -
m[2,2] ) ) / 2;
q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0,0] + m[1,1] -
m[2,2] ) ) / 2;
q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0,0] - m[1,1] +
m[2,2] ) ) / 2;
q.x * = Mathf.Sign(q.x * ( m[2,1] - m[1,2] ) );
q.y * = Mathf.Sign(q.y * ( m[0,2] - m[2,0] ) );
q.z * = Mathf.Sign(q.z * ( m[1,0] - m[0,1] ) );
return q;
}
function Awake()
{
this.animation.wrapMode = WrapMode.Loop;
...
Destroy(aimPoint.collider);
this.animation.Play("AimStraight");
}
function Update ()
{
...
if(Input.GetKeyUp("up")| |Input.GetKeyUp("down") ||
Input.GetKeyUp("right shift"))
{
}
}
function LateUpdate()
{
//IK ----
//determine relative location between gun and hand
// and store it.
rightHandMat = ChangeCoordinateSystems(handR, gun);
leftHandMat = ChangeCoordinateSystems(handL, gun);
gun.LookAt(aimPoint.transform.position);
gun.transform.Rotate(Vector3.up,90);
//place hand back relative with gun using stored matrix
handR.rotation = QuaternionFromMatrix(