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 /
This question was closed Jan 02, 2017 at 01:33 PM by Sawula for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Sawula · Dec 05, 2016 at 03:21 PM · androidcameraorbitgyroscope

Gyroscope Orbit Android

Hello everyone, I need a bit of help I got a camera in my scene as a child of a gameobject. I need the camera to rotate smoothly around its parent depending on the gyroscope.

Kinda like filming a box while turning around it. I have the script to rotate with the gyro on the parent object, but it work strangely because after a few turns the camera start going sideways and weird. Also, if try to turn more than 180, the camera flip around, like the orientation is changing, except it's not. Anyone know a solution?

     using UnityEngine;
     using System.Collections;
     
     
     public class GyroCam : MonoBehaviour {
     
     
         private Quaternion initialRotation;
         private Quaternion gyroInitialRotation;
     
     
         void Start ()
         {
             Input.gyro.enabled = true;
             initialRotation = transform.rotation; 
             //gyroInitialRotation = Input.gyro.attitude;
     
         }
     
     
         void Update () 
         { 
 //smooth gyro rotation
     float newPositionx = Mathf.SmoothDamp(transform.position.x, Input.gyro.rotationRateUnbiased.x, ref _velocity, damp_speed);
 
         float newPositiony = Mathf.SmoothDamp(transform.position.y, Input.gyro.rotationRateUnbiased.y, ref _velocity, damp_speed); 
 
 //rotate
         transform.Rotate (newPositionx, 0, newPositiony);
     
             }
             
     }
 
 
Comment
Add comment · Show 4
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 Glurth · Dec 05, 2016 at 04:24 PM 0
Share

I notice your rotate function call is passing in BOTH an x and a y value. I would have expected an "orbit" to be a circle around only ONE axis.

If you want the orbit to be in the XY plane, then use transform.Rotate(0,angle,0) to rotate "about" the z axis.

Regarding it flipping around: this is due to the fact that you feed the input directly into your rotate function. This input goes from -180 to +180, circularly (so after +180 the next angle is -179). I would suggest you store some kind of "CurrentRoation" value yourself, and use this value to detect how the input has CHANGED since last update.

avatar image Sawula Glurth · Dec 05, 2016 at 04:41 PM 0
Share

No no, I need an orbit around in height and in side so those I checked one by one to see which axis was needed where and the rotation that happen is the one I want

Storing the current rotation alright and I suppose a check to see if it goes beyond the 180 mark, and when it does I should correct the math inside that stored value before applying it?

avatar image Glurth Sawula · Dec 05, 2016 at 08:08 PM 0
Share
 transform.Rotate (newPositionx, 0, newPositiony);

this function adjusts the current rotation of the object by the amount you specify in the parameters (it "rotates" it). This is where I was suggesting you should use the "rotationChange" value.

Alternatively, and perhaps more simply, SET, rather than ADJUST the rotation of the object:

 transform.localRotation = Quaternion.Euler(newPositionX,0,NewPositionY);


Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by cakeslice · Dec 05, 2016 at 09:35 PM

The gyroscope by itself is unreliable, you will get drift over time. To fix this developers use sensor fusion (a combination of gyroscope & accelerometer)

Here's a great post about it: http://smus.com/sensor-fusion-prediction-webvr/

Also, try this example on your phone, it uses sensor fusion: https://borismus.github.io/webvr-boilerplate/

And here's a Unity port of that code: https://github.com/cakeslice/UnitySensorFusion

Comment
Add comment · Show 7 · 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 Sawula · Dec 09, 2016 at 10:33 AM 0
Share

I read the website, and it does indeed explain why my gyro script has this "drafting" that make it go weird. I tried the script from that project, and while it does indeed work smoothly and with no offset, the results are not what I expected, and I don't really understand the script itself sadly :c (I never coded for android before and I'm more used to simpler projects)

The rotation is way too slow, it doesn't follow the phone movement fast enough. Not all the axes have a rotation either... If anyone know where in the script I'm can try and change that, it would be great...

avatar image cakeslice Sawula · Dec 10, 2016 at 05:06 PM 0
Share

That script is $$anonymous$$e, I think there was a bug which is now fixed, can you download the latest version and try again please?

(I don't have a phone with a gyro so I can't test it)

avatar image Sawula cakeslice · Dec 12, 2016 at 09:06 AM 0
Share

Oh I hadn't seen

The speed is now much better, I still have a bit of a hard time understanding where and how you tell the script which axis of the phone gyro correspond to which axis of the unity scene but this is already much better thanks!

if I can bother you again: I still have a flip of the camera when I pass the 180° on unity y axis. (it's the axis on the width of the phone, in landscape mode) Do you know how I could remove that flip?

Show more comments
avatar image Sawula · Dec 30, 2016 at 02:39 PM 0
Share

Don't know why, Unity wouldn't let me add more comments to our discussion, so continuing here Sorry for the long silence, holidays etc, I didn't touch this much, but no, after building your test scene and trying it out, there is no flip on it at all on the cube.

Do you think it could be because it's a camera that I'm using the gyroscope onto? Or maybe it's the way I use to lock the camera on the scene (LookAt)?

I found the lines for the input, so now I'm trying them one by one, not difficult job, just long XD

EDIT: got it to work, it was my lookat that did the tilting, so I'll solve this another way thanks a lot again for the help :)

avatar image TBarretH · Mar 04, 2018 at 10:17 PM 1
Share

Hey CakeSlice, I'm very interested in the sensor fusion code you shared for this answer but the github repositories seem to have been deleted. Is there anyway you could share that code again?

Thanks, Barret

avatar image ChristophGeske TBarretH · Jan 30, 2019 at 06:03 PM 0
Share

I would be interested in the Unity version of the sensor fusion as well. I have not seen a working sensor fusion project for android made in unity so far.

Follow this Question

Answers Answers and Comments

120 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

Related Questions

create 3d camera using gyroscope android 0 Answers

Gyroscope smooth transitions (minimizing jerk from tiny movements) 1 Answer

Android gyroscope 4 Answers

Rotating a camera using the gyroscope 4 Answers

Android Unity ARCore camera access 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