Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Karsnen_2 · Jan 18, 2017 at 03:32 AM · rotationvrmobilemathrotations

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Control movement with head rotation in cardboard 0 Answers

Rotate camera like VR on mobile 3 Answers

VR Scene Rotation on Build 0 Answers

Which is the correct way to rotate the XRrig? 1 Answer

Can we manually set a rotation and a position for the XR rig / VR Headset Oculus Rift ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges