This site uses strictly necessary cookies. More Information

X# Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion

So basically like this gif looks like https://giphy.com/gifs/l0MYrjDwyvzLmj7Ms

^ left box represents gyroscope, right box represents player camera

here is the working code with eulerAngles,

```
gyroRotation = Input.gyro.attitude;
gyroRotation = Vector3.SlerpUnclamped(previousGyroscopeRotation, gyroRotation, fDamping * Time.deltaTime);
deltaRotation = (previousGyroscopeRotation - gyroRotation);
finalRotation = finalRotation - deltaRotation;
if (finalRotation.x != 0)
finalRotation.x = Mathf.LerpUnclamped(finalRotation.x, 0, 1 * Time.deltaTime);
if (finalRotation.y != 0)
finalRotation.y = Mathf.LerpUnclamped(finalRotation.y, 0, 1 * Time.deltaTime);
if (finalRotation.z != 0)
finalRotation.z = Mathf.LerpUnclamped(finalRotation.z, 0, 1 * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(finalRotation), Time.deltaTime);
previousGyroscopeRotation = gyroRotation;
```

(but because of angle problems (ie: jumping from 350˙ to 10´ instead of 370)) internet suggested i use Quaternions, but have no idea how to achieve the same effect

```
transform.rotation = Quaternion.Slerp(transform.rotation, Input.gyro.attitude, Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, zeroPosition, Time.deltaTime);
```

this is the peak of my skills with Quaternions, if someone could shed some light on how to achieve this as simply as possible

**Answer** by Arshia001
·
Jul 23, 2016 at 04:25 PM

To solve your Euler angle problems, you can use Mathf.LerpAngle which takes the jump from 360 to 0 into account. Also, the equivalent of (0,0,0) for rotation is Quaternion.identity, since quaternions can never be zero. I don't think using angles is a bad idea, if you know what you're doing. Simply changing Lerp to LerpAngle should probably fix any problems you have.

**Answer** by Trstek
·
Jul 24, 2016 at 02:54 PM

Ok So I've used Mathf.LerpAngle, it did fix the angle problems I've had, but there were still the usual EulerAngle problems, where rotation x over ~75˙, y and z axis both flip to 180, and that again screwed the system up.

So I've research Quaternions, and basically few lines of code does exactly what I wanted with no problems :)

```
gyroRotQuat = Input.gyro.attitude;
deltaRotQuat = (previousGyroscopeRotQuat * (Quaternion.Inverse(gyroRotQuat)));
finalRotQuat = finalRotQuat * (Quaternion.Inverse(deltaRotQuat));
transform.rotation = Quaternion.Slerp(transform.rotation, deltaRotQuat, Time.deltaTime);
previousGyroscopeRotQuat = gyroRotQuat;
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers

Delayed LookAt, but only on one axis 1 Answer

Rotate on Transform.localeulerAngels smoothly 0 Answers

Smooth look at 0 Answers

Can't really grasp how to keep my player level on X and Z while using LookRotation 0 Answers