How to use phone gyroscope as steering wheel?
Hello, I am trying to control transform's rotation with Z axis of phone gyroscope. I have tried it using gyro.rotationRateUnbiased.z which in theory worked fine but after few tilts of device, rotation of device wasn't synchronized with gameobejct's rot. so probably there was some drift happening... Then I tried rotating transform using gyro.attitude.z but that only worked properly when my phone was oriented to north (because the attitude is based on compass, gyroscope and accelerometer (there is not much about it in documentation so I guess that's how it is working...)) When my device was oriented randomly (not to north) Z axis was mixed with X and Y. I have tried to measure gyroscope offset from north at start of program and then decrementing measured value from current gyro attitude value in runtime but z axis was still mixed with x and y. I have read probably all of the threads on internet about gyroscopes/accelerometers in unity and problems associated but nothing really worked for me :( So if anyone has experience with what I mentioned here, please give me your advice or if there is any plugin or something for this I would be really thankful for mentioning it:) Thanks a lot for every response.
Answer by ENDAS_ORIGINAL · Aug 11, 2018 at 11:41 AM
Finally I noticed gyro.gravity property and that works fine :)
void Rotation() {
float gravityOutputMultiplier = -50;
Vector3 currentRot = transform.eulerAngles;
transform.eulerAngles = new Vector3 (currentRot.x, currentRot.y, gyro.gravity.x * gravityOutputMultiplier);
}
Also gyro.gravity in my case uses both accelerometer and gyroscope probably for better accuracy I guess, but the output is still a little shaky so I suggest to process it with moving avarage of last 3-7 output values to make it more stable
Wow, this is exactly what I needed to just detect steering type of tilt. I don't know how this is not in the other most visible posts about this topic. Thanks!
Answer by narlankaaditya · Apr 29, 2021 at 08:08 AM
@ENDAS_ORIGINAL will this script make my steering wheel rotate as much as I tilt my phone?
Yes, maybe adjust the multiplier(sensitivity) value a little if the steering wheel is turning faster or slower than the phone. It is also possible that the change in gyro.gravity.x is not directly proportional to change of phone's tilt in degrees but you can fix that with goniometric math functions. And obviously you have to assign the new Vector3 value to correct transform also u have to call this function from Update() and declare and assign your gyro probably in Start().
Your answer
