Accelerometer axis directions?

The documentation states:

If you hold the device upright (with the home button at the bottom) in front of you, the X axis is positive along the right, the Y axis is positive directly up, and the Z axis is positive pointing toward you.

But then again, with simple code:

Debug.Log("x = " + Input.acceleration.x + ", y = " + Input.acceleration.y
    + ", z = " + Input.acceleration.z);

I found it not to be true (the axes were completely different). But surprisingly enough, when earlier I assumed that the documentation was right and implemented a simple shake along Z axis, it worked (yes, on the same device).

So my question is - what the hell? What are the directions of axes? Are they the same on all devices?

I´ve had the same problem and I solved it by setting the app to Landscape mode in the player settings and then set the screen orientation to portrait in a script like this:

function Start() {
Screen.orientation = ScreenOrientation.Portrait;
}

I don not know why you have to do this but it worked for me on the X axis. I don´t know about the Z axis though.

Ok, so apparently Unity failed with their documentation - they didn’t mention that the axes readings from accelerometer depend on the Screen.orientation parameter. If it’s set to Portrait then the description in the documentation is true, if it’s set e.g. to Landscape, the “top” or “up” of the phone is one of its sides and the axes change accordingly.

I’d be glad if anyone confirmed/refuted that that’s the case in iOS too.

For me it works as specified. However before Unity4 i guess the accelerometer was always relative to the device. In the current version the accelerometer axis rotates along with the screen.

  • If i hold it upright i get a value of (0, -1, 0).
  • If i tilt it to the right i get (1,0,0). Of course only if autorotate is off :wink:
  • If i lay it flat on the table i get (0,0,-1)

This is exactly what the docs said, maybe you are confused what you actuall get back from the accelerometer?

If you don’t move your device the only acceleration the device measures is the gravity vector which of course points to the ground in our real world :wink: So when holding it upright the gravity pulls down on the y-axis, that’s why you get -1

Btw: tested on Nexus7

Here’s my MobileStats.apk if you’re interested. It shows some device stats, touch positions gui mouse position, acceleratometer, …

Unfortunately since Unity4 Unity sets the gui position to a huge number when you release all fingers. That’s a bit annoying, but it still works :wink: