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 · May 29, 2017 at 05:53 PM · camerarotationvrmobilegyroscope

Rotate camera like VR on mobile

Hi, i'm working on a mobile/vr app that the user can look around in 360. When working in VR I simply activate the built in solution in Unity and everything works fine. The built in VR mode handles perfectly the gyroscope and rotates the camera to the right position on any smartphone I tested that had support.

My problem is that I can't use that rotation outside of VR mode, so I need to make my own implementation for rotating the camera using the gyroscope. I tried using Input.gyro.attitude and it works perfectly on the IOS devices I tested, but on Android devices everything falls apart. On some the gyroscope is glitchy and acting strange, on others the gyroscope is receiving input from acceleration and that is super weird. I moved to Input.gyro.rotationRateUnbiased for the rotation, it is better, but it starts to lose it's orientation after a while and if I shake the device every thing goes crazy and I end up in a random rotation.

Is there a simple solution for this? Why does this happen only on Android devices? Can i get the VR rotation even outside of VR?

If anyone can help me I'd be really grateful.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ismaelnascimento01 · May 31, 2017 at 05:35 PM

Check link: https://www.youtube.com/watch?v=HIduNSjAQjU and https://docs.unity3d.com/ScriptReference/Input-acceleration.html

Comment
Add comment · Show 2 · 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
avatar image · May 31, 2017 at 10:32 PM 0
Share

thx for the reply. This is not what I want to do. I need de orientation in space of the device, not the acceleration.

avatar image ismaelnascimento01 · Jun 01, 2017 at 07:56 PM 0
Share

Try it: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

avatar image
0

Answer by abdulthegamer · Jun 09, 2017 at 03:33 AM

Hi,

If you have already implemented VR mode then just

  • disable VR in GVR viewer gameobject or prefab and it will handle Gyro automatically. No need to implement again for standard mode.

  • Or Just Disable like below if you using Google VR package.

GvrViewer.Instance.VRModeEnabled = false;

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
avatar image
0

Answer by HanSoloYolo · Jul 23, 2017 at 09:52 PM

Thank you for your answers, but I believe you are misunderstanding the question.

This is the code you need to have VR without a Headset. Now, I am trying to figure out how to have 2 finger touches reset the camera to it's original orientation offset from the phones current orientation. So if you were lying down and aiming the phone up, you can tap 2 fingers and set the view to your current gyro.attitude. How would you modify the below to do so?

  1. Create an Empty Game Object and name it "Gyro Control". You can rename it, but you would also have to do so in the code following.

  2. Drag Main Camera onto Gyro Control. Now the Gyro Control is the parent object.

  3. Right-Click on your Assests folder in Project View and select Create > C# Script. Name it GyroController.

  4. Erase everything and cut & paste the following code:

"using UnityEngine; public class GyroController : MonoBehaviour { private bool gyroEnabled; private Gyroscope gyro; private GameObject GyroControl; private Quaternion rot;" +

 private void Start () 
 {
     GyroControl = new GameObject ("Gyro Control");
     GyroControl.transform.position = transform.position;
     transform.SetParent (GyroControl.transform);
     gyroEnabled = EnableGyro();

 }


 private bool EnableGyro()
 {
     if (SystemInfo.supportsGyroscope) 
         {
         gyro = Input.gyro;
         gyro.enabled = true;
         GyroControl.transform.rotation = Quaternion.Euler(90f, -90f, 0f);
         rot = new Quaternion(0, 0, 1, 0);
         return true;
         }
     return false;
 }

 private void Update () 
 {
     if (gyroEnabled) 
     {
         transform.localRotation = gyro.attitude * rot;
     }
 }

}

Now... how to get it where if (Input.touchCount == 2) the scene recenters on your current gyro.attitude?

It has something to do with Quaternion.identity, rot, gyro.attitude, if (Input.touchCount == 2) {transform.localRotation = Quaternion.identity * gyro.attitude}, or who knows?

This way you can recalibrate the game to whatever angle you are currently facing.

Comment
Add comment · Show 2 · 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
avatar image HanSoloYolo · Jul 24, 2017 at 03:45 AM 0
Share

So, I hired a tutor and figured it out. The following works!

 using UnityEngine;
 public class GyroController2 : $$anonymous$$onoBehaviour 
 {
     private bool gyroEnabled;
     private Gyroscope gyro;
     private GameObject GyroControl;
     private Quaternion rot;
     private Quaternion adjustrot;
 
     private void Start () 
     {
         GyroControl = new GameObject ("Gyro Control");
         GyroControl.transform.position = transform.position;
         transform.SetParent (GyroControl.transform);
         gyroEnabled = EnableGyro();
         adjustrot = Quaternion.Euler(90f, 0f, 0f) * Quaternion.Inverse (gyro.attitude);
     }
 
 
     private bool EnableGyro()
     {
         if (SystemInfo.supportsGyroscope) 
             {
             gyro = Input.gyro;
             gyro.enabled = true;
             GyroControl.transform.rotation = Quaternion.Euler(90f, -90f, 0f);
             rot = new Quaternion(0, 0, 1, 0);
             return true;
             }
         return false;
     }
 
     private void Update () 
     {
         if (gyroEnabled) 
         {
             if (Input.touchCount == 3) 
             {
                 adjustrot = Quaternion.Euler(90f, 0f, 0f) * Quaternion.Inverse (gyro.attitude);
             }
             transform.localRotation = adjustrot * gyro.attitude * rot;
         }
     }
 }
avatar image RealAnimaGames HanSoloYolo · Sep 17, 2017 at 10:13 AM 0
Share
@InventorDerek First thank so much for your code. I'm was looking for that way of moving camera and the firs part works perfectly. Now I'm trying to the reset position, there is some point on my app where I stop the gyro and force the camera to be again at 0,0,0 the reenable the gyro and want to restart gyro movement from this point. With your 'adjustrot' my camera movement don't work exactly like the previous part with just the 'attitude' and the 'rot'. I also tried with the offset of this post http://answers.unity3d.com/questions/289184/gyro-quaternion-offset.html But again the rotation is not the same and don't work with the camera position reset. Any idea on how to reset the camera and reenable the gyro? I´m a little lost right now. Thanks so much!

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

181 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 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

Using gyroscope to control a camera? 1 Answer

Switch between touchscreen and Google VR to control the Unity camera 1 Answer

Control movement with head rotation in cardboard 0 Answers

I have some Rotation problems 1 Answer

Problems with gyro camera rotation. 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