Gyroscope, Gravity Vector mathematical problem

I’m stuck for a few days now and I just can’t wrap my mind around it. I want to keep the orientation of an Object fixed in real world 3D-space, making use of the gravity vector, that is provided by the gyroscope. Kinda like shown here @ min 1:30 https://www.youtube.com/watch?v=ORcu-c-qnjg.

I use Unity 3.5 and it would be OK, if I would get it working in just one orientation of the device (e.g. Portrait). I use the prime31 DeviceMotion-PlugIn, which gives me the values of Gravity X, Gravity Y, and Gravity Z, which I apply (multiplied by 90) to the eulerAngles of my Object. It looks promising but there is a mathematical problem involved, that I just don’t get.

Here’s my wrong approach, but just to describe the issue:
I apply the Gravity Y- value (by 90) to the rotation.x of my object. This keeps it looking up perfectly in real world 3D-space, when I tilt the device. But when I roll the device (without tilting) the Gravity Y- value does also change (and thus my object).

I know this is totally the wrong approach. But can anybody help me to bring the three Gravity-Values into the right mathematical context?

Thanks so much!

1 Like

I found this as a solution for my problem: iPhoneGyroCam - Pastebin.com
The script requests the Input.gyro.attitude which just needs to be fixed with the respective Quaternion of the current device orientation. I think I’ve read somewhere that it is fixed in Unity4.0. But I don’t know for sure about that.
(Input.isGyroAvailable is today: SystemInfo.supportsGyroscope)

In Portrait it would be:

var myObject : Transform;

function Update () {
	
	var gyro = Input.gyro;
	var rotFix = Quaternion(0,0,1,0); // fix for Portrait
	var camRot : Quaternion = gyro.attitude * rotFix;
	myObject.localRotation = camRot;
}

My Camera would then be a child of this object and look towards it. And my Tower would just be a fix Object in Editor space.

(The Gyroscope is by default enabled with 60Hz in the Player Settings)

1 Like

Many thanks! :slight_smile: this script is great help for my project! :slight_smile: