Rotating an object using the accelerometer from an Android device

I’m currently trying to control the tilt of a simulated toy “airplane” with my android device, and seeing if I can implement tilt functionality. I’m just trying to rotate it along the Y axis at the moment.

My first implementation tried to rotate the object by adding the value from input.acceleration to the Y rotation.

  if(movement.normalizedDir.y >= 0.2 || movement.normalizedDir.y <=-0.2){
    transform.Rotate(0,movement.normalizedDir.y*Time.deltaTime*rotationSpeed*-1,0);
	}

This worked quite well, but I found out that it felt more intuitive if the “airplane” rotated towards the angle that I held my phone, rather than rotating faster the more I tilted the phone. I tended to hold it up towards the intended end rotation.

So then I’m looking for a way to rotate from my current rotation, towards the rotation I am holding my phone, over time. I was looking at Quaternion.Slerp, which needs two quaternions.

As my knowledge of quaternions is rather slim I was wondering if I could convert the vector from input.acceleration to a quaternion and somehow feed that to the end quaternion in slerp. Any alternative suggestions on how to solve it will of course be highly appreciated.

Sincerely

EvilNorwegian

If you’re looking to match the angle at which you’re holding the phone, I’d use Input.gyro.attitude rather than linear acceleration. And that’s a quaternion anyway, so you’ll find slerping easier.