Reputation: 29
I use this code to track the movement of the gyroscope and rotate the object:
Vector3 previousEulerAngles = transform.eulerAngles;
Vector3 gyroInput = -Input.gyro.rotationRateUnbiased;
Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
targetEulerAngles.x = 0.0f;
targetEulerAngles.y = 0.0f;
transform.eulerAngles = targetEulerAngles;
Everything works smoothly and almost the way I need, but there are 2 problems. The first is that when the smartphone is sharply rotated and when it is rotated 360 degrees or more, the object begins to roll away from its initial position. A collapse effect appears.
The second problem is that if the device is rotated before starting the application, then the rotation of the object starts from the initial position, and not from the position of the gyroscope. I’ve spent a few days already solving these problems.
Maybe someone has already encountered such a problem?
Upvotes: 0
Views: 332
Reputation: 90852
For fixing the initial orientation you could once apply Gyroscope.attitude
to your objects orientation.
Again if you only want to rotate the object on the Z
axis the easiest way would probably be something like e.g.
private void Start()
{
var attitude = Input.gyro.attitude;
var targetEulerAngles = attidue.eulerAngles;
targetEulerAngles.x = 0;
targetEulerAngles.y = 0;
transform.eulerAngles = targetEulerAngles;
}
Further the value returned by Gyroscope.rotationRateUnbiased
already is a frame-rate independent rotation
in radians per second
so the multiplication by Time.deltaTime
is not required / not correct here.
Indeed you could instead of going incremental even just always directly apply the actual current Gyroscope.attitude
private void Update()
{
var attitude = Input.gyro.attitude;
var targetEulerAngles = attidue.eulerAngles;
targetEulerAngles.x = 0;
targetEulerAngles.y = 0;
transform.eulerAngles = targetEulerAngles;
}
Upvotes: 0