Last active
January 28, 2016 15:56
-
-
Save paulhayes/f50aba2f90eaa7b6b87e to your computer and use it in GitHub Desktop.
For Unity3d. Intended to be used as a member property of component. Simple Gyro input controller that tracks the relative rotation of the device while the screen is being touched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [System.Serializable] | |
| public class GyroControls{ | |
| public Vector3 clampRotation = new Vector3(30f,30f,30f); | |
| public Vector3 deadZone = new Vector3(3f,3f,3f); | |
| Quaternion startRotation; | |
| bool touching; | |
| Vector3 mRotation; | |
| public void Update(){ | |
| bool touchingNow = Input.touchCount > 0; | |
| Quaternion rot; | |
| Vector3 eulerRot; | |
| if( !touching && touchingNow ){ | |
| startRotation = Quaternion.Inverse( Input.gyro.attitude ); | |
| } | |
| touching = touchingNow; | |
| if( touching ){ | |
| rot = startRotation * Input.gyro.attitude; | |
| eulerRot = Quaternion.Inverse(rot).eulerAngles; | |
| } | |
| else { | |
| eulerRot = Vector3.zero; | |
| } | |
| if( eulerRot.x > 180 ) eulerRot.x -= 360; | |
| if( eulerRot.y > 180 ) eulerRot.y -= 360; | |
| if( eulerRot.z > 180 ) eulerRot.z -= 360; | |
| eulerRot.x = Mathf.Sign(eulerRot.x) * Mathf.Max(Mathf.Abs(eulerRot.x)-deadZone.x,0); | |
| eulerRot.y = Mathf.Sign(eulerRot.y) * Mathf.Max(Mathf.Abs(eulerRot.y)-deadZone.y,0); | |
| eulerRot.z = Mathf.Sign(eulerRot.z) * Mathf.Max(Mathf.Abs(eulerRot.z)-deadZone.z,0); | |
| eulerRot = Vector3.Min( eulerRot, clampRotation ); | |
| eulerRot = Vector3.Max( eulerRot, -clampRotation ); | |
| Vector3 input = new Vector3( eulerRot.x / clampRotation.x, eulerRot.y / clampRotation.y, eulerRot.z / clampRotation.z ); | |
| mRotation = input; | |
| } | |
| public Vector3 rotation { | |
| get { | |
| return mRotation; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment