Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Update()
- {
- Quaternion swing, twist;
- DecomposeQuat (transform.localRotation, Vector3.forward, out swing, out twist);
- float swing_angle, twist_angle;
- Vector3 swing_axis, twist_axis;
- swing.ToAngleAxis (out swing_angle, out swing_axis);
- twist.ToAngleAxis (out twist_angle, out twist_axis);
- swing_angle = Mathf.Clamp (swing_angle, 45.0F, 90.0F);
- twist_angle = Mathf.Clamp (twist_angle, 45.0F, 90.0F);
- transform.localRotation = Quaternion.AngleAxis (swing_angle, swing_axis) * Quaternion.AngleAxis (twist_angle, twist_axis);
- }
- private Quaternion Normalize(Quaternion q)
- {
- float dot = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
- float invLength = 1.0F / Mathf.Sqrt (dot);
- return new Quaternion (q.x * invLength, q.y * invLength, q.z * invLength, q.w * invLength);
- }
- private void DecomposeQuat(Quaternion rotation, Vector3 direction, out Quaternion swing, out Quaternion twist)
- {
- Vector3 v = new Vector3 (rotation.x, rotation.y, rotation.z);
- Vector3 p = Vector3.Project (v, direction);
- twist = Normalize(new Quaternion (p.x, p.y, p.z, rotation.w));
- swing = rotation * Quaternion.Inverse (twist);
- }
Advertisement
Add Comment
Please, Sign In to add comment