c# - Using the Android gyroscope in Unity3d, how can I set the initial camera rotation to the initial mobile device rotation? -


i want use android gyroscope perform head tracking on standard first person controller of unity3d. created short script rotates both parent node , camera child node of first person controller. script attached camera.

this script works well, rotates first-person view based on movements of mobile device. however, works when hold phone in forward looking position when start app. if phone lies flat on table , start app, both camera , gyroscope rotations off.

i script respect initial device rotation. when start app , device has screen up, camera should up. how can modify script set camera rotation initial mobile device rotation?

using unityengine; using system.collections;  // activate head tracking using gyroscope public class headtracking : monobehaviour {     public gameobject player; // first person controller parent node     public gameobject head; // first person controller camera      // use initialization     void start () {         // activate gyroscope         input.gyro.enabled = true;     }      // update called once per frame     void update () {         // rotate player , head using gyroscope rotation rate         player.transform.rotate (0, -input.gyro.rotationrateunbiased.y, 0);         head.transform.rotate (-input.gyro.rotationrateunbiased.x, 0, input.gyro.rotationrateunbiased.z);     } } 

just save initial orientation in 2 variables, code become :

using unityengine; using system.collections;  // activate head tracking using gyroscope public class headtracking : monobehaviour {     public gameobject player; // first person controller parent node     public gameobject head; // first person controller camera      // initials orientation     private int initialorientationx;     private int initialorientationy;     private int initialorientationz;      // use initialization     void start () {         // activate gyroscope         input.gyro.enabled = true;          // save firsts values         initialorientationx = input.gyro.rotationrateunbiased.x;         initialorientationy = input.gyro.rotationrateunbiased.y;         initialorientationz = -input.gyro.rotationrateunbiased.z;     }      // update called once per frame     void update () {         // rotate player , head using gyroscope rotation rate         player.transform.rotate (0, initialorientationy -input.gyro.rotationrateunbiased.y, 0);         head.transform.rotate (initialorientationx -input.gyro.rotationrateunbiased.x, 0, initialorientationz + input.gyro.rotationrateunbiased.z);     } } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -