Minor issue with a VR head tracking script to control the camera?
Hello,
INTRO :
I have a head tracking script for VR to rotate around. It is placed on the Camera GameObject & is sole purpose is to rotate the camera in accordance to the rotation of the head.
ISSUE: The script woks fine but it looks like the rotation is inverted.
Target :
Mobile device (let's say iOS & Android)
REQUEST:
I think I am making a mistake with the math functions.
Here is the script :
 using UnityEngine;
 using System.Collections;
 
 public class HeadTrack : MonoBehaviour
 {
     private bool gyroBool;
     private Gyroscope gyro;
     private Quaternion rotFix;
     private Vector3 initial = new Vector3(90, 180, 0);
         
     // Use this for initialization
     void Start()
     {
         Screen.orientation = ScreenOrientation.LandscapeLeft;
         Screen.sleepTimeout = SleepTimeout.NeverSleep;
 
         gyroBool = SystemInfo.supportsGyroscope;
 
         Debug.Log("gyro bool = " + gyroBool.ToString());
 
         if (gyroBool)
         {
             gyro = Input.gyro;
             gyro.enabled = true;
 
             rotFix = new Quaternion(0, 0, 0.7071f, 0.7071f);
         }
         else
         {
             Debug.Log("No Gyro Support");
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         if (gyroBool)
         {
             var camRot = gyro.attitude * rotFix;
             transform.eulerAngles = initial;
             transform.localRotation *= camRot;
         }
     }
 }
If anyone could point me in the right direction, I would much appreciate it.
regards, Karsnen.
               Comment
              
 
               
              Answer by Karsnen_2 · Feb 07, 2017 at 03:26 AM
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class GyroCamera : MonoBehaviour
 {
     private float initialYAngle = 0f;
     private float appliedGyroYAngle = 0f;
     private float calibrationYAngle = 0f;
 
     private Camera myCamera;
 
     [SerializeField]
     internal GameObject[] allCubes;
 
     private void Start()
     {
         #if UNITY_ANDROID
         Input.gyro.enabled = true;
         #endif
 
         Application.targetFrameRate = 60;
         initialYAngle = transform.eulerAngles.y;
         CalibrateYAngle ();
     }
 
     private void OnEnable()
     {
         if(myCamera == null)
         {    myCamera = this.GetComponent<Camera> ();}    
     }
 
 
     private void OnDisable()
     {}
 
     void Update()
     {
         ApplyGyroRotation();
         ApplyCalibration();
     }
 
     bool cubeHit = false;
     private void FixedUpdate()
     {
         
         RaycastHit _raycastHit = new RaycastHit(); // create new raycast hit info object
 
         // "vrdetection" layer = 8
         if(Physics.Raycast (this.transform.position, transform.forward, out _raycastHit, Mathf.Infinity, 1 << 8))
         { 
             _raycastHit.transform.GetComponent<Renderer> ().material.color = Color.red;    
             cubeHit = true;
         } 
         else
         {
             if(cubeHit == true)
             {
                 foreach (GameObject _go in allCubes)
                 {
                     _go.GetComponent<Renderer> ().material.color = Color.white;
                 }
                 cubeHit = false;
             }
         }
 
     }
 
     public void CalibrateYAngle()
     {
         calibrationYAngle = appliedGyroYAngle - initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
     }
 
     void ApplyGyroRotation()
     {
         this.transform.rotation = Input.gyro.attitude;
         appliedGyroYAngle = this.transform.eulerAngles.y; // Save the angle around y axis for use in calibration.
 
         #if UNITY_ANDROID
 
         this.transform.Rotate( 0f, 0f, 180f, Space.Self ); //Swap "handedness" ofquaternionfromgyro.
         this.transform.Rotate( 270f, 180f, 180f, Space.World ); //Rotatetomakesenseasacamerapointingoutthebackofyourdevice.
 
         #else
 
         this.transform.Rotate ( 0f, 0f, 180f, Space.Self ); //Swap "handedness" ofquaternionfromgyro.
         this.transform.Rotate ( 90f, 180f, 0f, Space.World ); //Rotatetomakesenseasacamerapointingoutthebackofyourdevice.
 
         #endif
     }
 
     void ApplyCalibration()
     {
         transform.Rotate( 0f, -calibrationYAngle, 0f, Space.World ); // Rotates y angle back however much it deviated when calibrationYAngle was saved.
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                