- Home /
The question is answered, right answer was accepted
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);
}
}
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.
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?
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);
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
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...
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)
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?
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 :)
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
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
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