- Home /
New Gyroscope Controls in Unity 4?
WHY UNITY WHY? I had just got this script for a gyroscope controlled main camera online, and it was working well. Maybe .1 second lag with the gyroscope, but it was working. I had just made a few edits, otherwise it was the original. When I used the same script in a different, the gyroscope is basically messed up. It flipped the game 90 degrees, and when I tilt it upwards, it moves the camera left. When I tilt it downwards, it goes right. When I move it right, it goes up, and vice versa. Any help would be really really greatly appreciated. I'm stumped, the only change I've made is upgraded to unity 4, maybe that could have something to do with it? Thanks,
Here is the script by the way:
// iPhone gyroscope-controlled camera demo v0.3 8/8/11
// Perry Hoberman <hoberman@bway.net>
// Directions: Attach this script to main camera.
// Note: Unity Remote does not currently support gyroscope.
private var gyroBool : boolean;
private var gyro : Gyroscope;
private var rotFix : Quaternion;
function Start() {
var originalParent = transform.parent; // check if this transform has a parent
var camParent = new GameObject ("camParent"); // make a new parent
camParent.transform.position = transform.position; // move the new parent to this transform position
transform.parent = camParent.transform; // make this transform a child of the new parent
camParent.transform.parent = originalParent; // make the new parent a child of the original parent
gyroBool = Input.isGyroAvailable;
if (gyroBool) {
gyro = Input.gyro;
gyro.enabled = true;
if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
camParent.transform.eulerAngles = Vector3(90,90,0);
} else if (Screen.orientation == ScreenOrientation.Portrait) {
camParent.transform.eulerAngles = Vector3(90,180,0);
}
if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
rotFix = Quaternion(0,0,0.7071,0.7071);
} else if (Screen.orientation == ScreenOrientation.Portrait) {
rotFix = Quaternion(0,0,1,0);
}
//Screen.sleepTimeout = 0;
} else {
print("NO GYRO");
}
}
function Update () {
Invoke ("Rotate", 12.8);
}
function Rotate () {
if (gyroBool) {
var camRot : Quaternion = gyro.attitude * rotFix;
transform.localRotation = camRot;
}
}
Answer by headhunter45 · Nov 02, 2013 at 06:37 AM
Once the gyro is returning good data do this once to figure out the initial rotation
inverseInitialRotation = Quaternion.Inverse(Input.gyro.attitude);
Then every frame after that
Quaternion deltaQ = Input.gyro.attitude * inverseInitialRotation;
transform.localRotation = Quaternion.Euler(-deltaQ.eulerAngles.y, -deltaQ.eulerAngles.z, deltaQ.eulerAngles.x);
You may have to swap the euler values differently depending on your camera's orientation in the scene.
"You may have to swap the euler values differently depending on your camera's orientation in the scene" - Yeah, that's the part that's the pain. And I find it also differs between iOS and Android.
Various suggestions include:
Rotate the gyro attitude reading by a constant quaternion (0,0,1,0) and rotate its parent object by a constant angle of (90,180,0) for all device orientations (Post from “Aroha” in http://forum.unity3d.com/threads/98828-sharing-gyroscope-controlled-camera-on-iPhone-4/page5)
Rotate the gyro attitude by eulerangles (90, 0, 90) (Post by “George Foot” in http://forum.unity3d.com/threads/128493-$$anonymous$$atch-Unity-camera-with-iPhone-camera)
Rotate the gyro attitude dependent on screen orientation – Z rotation of –90 for Landscape Left, +90 for Landscape Right, or 180 for Portrait Upside Down, and also invert the z and w coordinates of the attitude quaternion to be (x, y, -z, -w) (http://blog.heyworks.com/how-to-write-gyroscope-controller-with-unity3d/)
What worked for me was to invert the w and z aspects of the quaternion attitude, i.e.:
rotFix = new Quaternion(gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
do you put this rotFix in for both in the code above? I'm still having issues with getting landScaleLeft to work properly. I've tried numerous rotation combinations in this part of the code, but to no avail...
camParent.transform.eulerAngles = Vector3(90,90,0);
anyone have values they $$anonymous$$NOW work? I'm up for trying more :D
thanks in advance!!!
note: the calculation work properly in Portrait mode, whereby i can just turn my phone and then go from there, but my GUI is then off ;)
Answer by Oneiros90 · Oct 28, 2015 at 01:21 PM
What worked for me (tested on iPhone 6 with Unity 5.2):
Quaternion rotFix1 = Quaternion.Euler(90, 90, 0);
Quaternion rotFix2 = Quaternion.Euler(0, 0, 180);
camera.localRotation = rotFix1 * Input.gyro.attitude * rotFix2;