Recently in a virtual reality project, I've been looking for a gyroscope article in unity. The following code is I find out on the Internet, when I use it can not be used. Now this is a modified one, although the copy can be used, very convenient.
The using unityengine;using system.collections;//camera Gyroscope rotates the public class iphonegyro:monobehaviour{//bool Gyrobool; Gyroscope Gyro; Quaternion Quatmult; Quaternion Quatmap; UILabel ul; Gameobject player; Gameobject camparent; void Awake () {player = Gameobject.find ("player"); Find the current parent of the camera ' s transform transform currentparent = transform.parent; Instantiate a new transform camparent = new Gameobject ("Camparent"); Match the transform to the camera position camParent.transform.position = transform.position; Make the new transform the parent of the camera transform transform.parent = camparent.transform; Make the original parent the grandparent of the camera transform//camparent.transform.parent = currentparent; Instantiate a new transform gameobject camgrandparent = new Gameobject ("Camgrandparent"); Match the transform to THe camera position camGrandparent.transform.position = transform.position; Make the new transform the parent of the camera transform camParent.transform.parent = camgrandparent.transform; Make the original parent the grandparent of the camera transform camGrandparent.transform.parent = Curre Ntparent; Gyrobool = true; if (gyrobool) {gyro = Input.gyro; UL = Gameobject.find ("Rotation"). Getcomponent<uilabel> (); Gyro.enabled = true; CamParent.transform.eulerAngles = new Vector3 (90,0, 0); Quatmult = new quaternion (0, 0, 1, 0); } void Update () {quatmap = new quaternion (gyro.attitude.x, Gyro.attitude.y, Gyro.attitude.z, Gyro.attitu DE.W); quaternion qt=quatmap * QUATMULT; Transform.localrotation =QT; Ul.text = "Rotation:" + transform.localrotation + "player" + player.transform.localRotation; } }
Perfect running on Android
Reprint Please specify: http://blog.csdn.net/dzc372787439
The use of "Little Super _u3d" Unity3d Gyro