- Home /

# Standard assets MouseLook.cs script question

Hi there :)

I've recently started tinkering with Unity`s standard assets and one function in MouseLook.cs really interested me:

```
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
return q;
}
```

These are the lines that I really do not understand:

```
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
```

I presume that the rotation is being reset here since q.w is 1 (cos(0/2) == 1) , but what these divisions do: q.x /= q.w; q.x /= q.w; q.z /= q.w;. Can someone clarify this?

This is a "normalization" of the rotation quaternion: the 4th element, w can be interpreted as a "weight" to the quaternion, so dividing all the elements with it you get a "normalized" quaternion (similar to how if you divide a Vector3 with its length ( `magnitude`

) you get a vector that points in the same direction, but has a length of 1). You can also interpret the last line `q.w = 1.0;`

as if you divided it with itself: `q.w /= q.w;`

, but setting it to 1.0 is just faster and more precise.

This normalization is needed for the second part where you get the x rotation. If you didn't normalize, you wouldn't know what is the rotation along the x axis, because it can be multiplied by an arbitrary "weight".

No, that's not normalizing. The resulting quaternion is no longer a unit quaternion. Unity automatically normalizes the quaternion when you assign a rotation. Normalization works the same way as it works for vectors. So you just divide all components by the "norm" or the quaternion (which is the same as the magnitude for vectors).

What they do here is essentially calculating "sin / cos" which is the tangent of half the angle. "w" is the real part while x,y and z are the imaginary vector part.

Though i'm not sure if this calculation actually works as it's always quite tricky to actually work directly with quaternions. It's actually quite similar to the axis - angle decomposition

I've just did a test and this clamping does only work under certain circumstances. It does preserve the rotation when still inside the specified range. However when the clamping kicks in the rotation can get messed up. It works properly when the rotation only represents a rotation around the x axis. However if there's also a rotation around y the result is wrong. So this only work for a single rotation around the worldspace x axis

If you don't know Quaternion math "Dont touch it values directly". Quaternion

### 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

Multiple Cars not working 1 Answer

Custom script editor (good practices), 0 Answers

Make character follow mouse 3 Answers