- Home /
Rotate object to match phone's orientation
Hi! I'm trying to rotate an object on its z axis to directly match the phone's orientation.
private void Update()
{
transform.rotation = Quaternion.Euler(0, 0, -Input.acceleration.x * 90);
}
This code rotates the object 90º on the z axis when I tilt the phone to the left, -90º when I tilt the phone to the right and 0º when the phone is facing upwards, but the problem is that in the way I've implemented it will never rotate more than -90º or 90º (because Input.acceleration only gives a value between -1 and 1).
I know a similar effect could be achieved by using a gyroscope but my phone doesn't have one.
In summary, is there any way I can make an object directly match the phone's rotation using my phone's accelerometer?
Answer by JaMG · Jul 26, 2018 at 12:21 AM
I found a soultion! After some time looking at the values of Input.acceleration while I was moving my phone I found out that Input.acceleration.y also changed when I was changing my phone's orientation. All I had to do was use those values (Input.acceleration x and y) in conjunction to allow for the 360º rotation.
I guess I didn't fully understand how the accelerometer works, so I'll go ahead and learn some more about this component =D.
Your answer
Follow this Question
Related Questions
[Mobile AR] Model faces away from camera? 0 Answers
Rotate object with accelerometer? 0 Answers
Calibrating Accelerometer Values 1 Answer
Get Accelerometer to rotate 360 degrees 2 Answers
Android Controls Help! 2 Answers