- Home /
Question by
TakeshiSaito · Jan 06, 2020 at 08:57 AM ·
gyro
Gyro Rotation
Hello
I got into trouble when I tried to rotate an image according to gyro input.
I implemented the code below, intending to rotate the image only Y Axis.But, whenever i rotate my device upword, the image starts to rotate.
I cannot figure out what is the problem. I`d be glad if you know something to solve this problem.
```
private void Update()
{
if (!Input.gyro.enabled) return;
var gyro = Input.gyro.attitude;
if (!initial.HasValue)
{
initial = gyro;
}
gyro = initial.Value * Quaternion.Inverse(gyro);
var gyroY = Quaternion.AngleAxis(gyro.eulerAngles.y, Vector3.up);
var roll = Mathf.Atan2(
2 * gyroY.y * gyroY.w + 2 * gyroY.x * gyroY.z,
1 - 2 * gyroY.y * gyroY.y - 2 * gyroY.z * gyroY.z
);
rotY = Mathf.SmoothDamp(rotY, roll, ref velocity, smooth, Mathf.Infinity, Time.deltaTime);
transform.rotation = Quaternion.Euler(0, Mathf.Clamp(rotY, -rotationRange, rotationRange), 0);
}
```
Comment
Your answer
Follow this Question
Related Questions
Calling Physics Yourself 0 Answers
Gyroscope on windows phones. Enabled but no working 0 Answers
Clamp gyro rotation 0 Answers
Gyro difference between iOS , Android 1 Answer
input.acceleration - change the zero position of the phone? 0 Answers