- Home /

# How do I clamp my rotation?

The player is in first-person, as humanoid model. To other players, when they look up, their torso rotates slightly vertically. It works the same way when I look down. This can also be observed in the game "Rust", by Garry Newman. Here is my script, that is attatched to the spine.

```
var speed : float = 5;
var torsoRotation : float = 0;
function Update() {
torsoRotation = Input.GetAxis("Mouse Y") * speed;
Mathf.Clamp(torsoRotation, -90, 90);
transform.Rotate(0, 0, -torsoRotation, Space.Self);
}
```

It works, except for the clamp. I can infinitely roll my upper body. I am suspicious at the fact that in the inspector, my `torsoRotation`

is equal to 0. When I move the mouse up or down fairly quickly, it changes to about 0.5 or 1. When I stop using the mouse, it drifts back to 0. *What is happening*, and *how can I fix it?*

The only clamp you've implemented is each frame limit the movement between -90 and 90 degrees.

I think I see what you're saying! Do you know how I could actually limit the rotation that you see?

You would need to `Mathf.Clamp(gameObject.eulerAngles.y, -90, 90);`

Here is my new code:

var speed : float = 5; var torsoRotation : float = 0; var maximumRotation : float = 359; var minimumRotation : float = 80; var torso : GameObject;

function Update() { torsoRotation = Input.GetAxis("Mouse Y") * speed; Mathf.Clamp(torso.eulerAngles.y, maximumRotation, minimumRotation); torso.transform.Rotate(0, 0, -torsoRotation, Space.Self); } It still doesn't work. I can run the game, but errors show up every time I move the mouse. :/

**Answer** by whydoidoit
·
Mar 10, 2014 at 12:44 PM

Try this:

```
function Update() {
torsoRotation = Input.GetAxis("Mouse Y") * speed;
transform.Rotate(0, 0, -torsoRotation, Space.Self);
transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y, -90, 90);
}
```

Clamping just returns a value that you have to set into the rotation yourself. You need this after you've applied your rotation. However, this may still cause strange flicking, if so you might need to use a cleverer clamping function that understands angles.

It worked! However, I really don't know where to start when it comes to your "cleverer clamping function" idea. Could you lead me in the right direction? Thank you so much!

Well this is my ClampAngle function:

```
public static float ClampAngle(float angle, float min, float max)
{
angle = Mathf.Repeat(angle, 360);
min = Mathf.Repeat(min, 360);
max = Mathf.Repeat(max, 360);
bool inverse = false;
var tmin = min;
var tangle = angle;
if(min > 180)
{
inverse = !inverse;
tmin -= 180;
}
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
var result = !inverse ? tangle > tmin : tangle < tmin;
if(!result)
angle = min;
inverse = false;
tangle = angle;
var tmax = max;
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
if(max > 180)
{
inverse = !inverse;
tmax -= 180;
}
result = !inverse ? tangle < tmax : tangle > tmax;
if(!result)
angle = max;
return angle;
}
```

Thanks! I really don't know how to specifically implement this into my script though. I'm not quite this advanced in Unity. Do I change any variables? Do I paste it into my script? Sorry to bother!

You'd add that to a class and then just use:

```
var clampedAngle = TheClassYouAddedItTo.ClampAngle(someAngle, minimumAngle, maximumAngle);
```

I made a C# class/script, and I named it `clampingAngle`

. It looks like this:

```
using UnityEngine;
using System.Collections;
public class clampingAngle : MonoBehaviour {
public static float ClampAngle(float angle, float min, float max)
{
angle = Mathf.Repeat(angle, 360);
min = Mathf.Repeat(min, 360);
max = Mathf.Repeat(max, 360);
bool inverse = false;
var tmin = min;
var tangle = angle;
if(min > 180)
{
inverse = !inverse;
tmin -= 180;
}
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
var result = !inverse ? tangle > tmin : tangle < tmin;
if(!result)
angle = min;
inverse = false;
tangle = angle;
var tmax = max;
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
if(max > 180)
{
inverse = !inverse;
tmax -= 180;
}
result = !inverse ? tangle < tmax : tangle > tmax;
if(!result)
angle = max;
return angle;
}
}
```

Is this correct? Then I added the variable into my mouseRotate script, that rotates the torso:

```
var speed : float = 5;
var torsoRotation : float = 0;
var maximumRotation : float = 359;
var minimumRotation : float = 80;
var clampedAngle = clampingAngle.ClampAngle(someAngle, minimumRotation, maximumRotation);
function Update() {
torsoRotation = Input.GetAxis("Mouse Y") * speed;
transform.Rotate(0, 0, -torsoRotation, Space.Self);
transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y, -90, 90);
}
```

How do I use the function/variable? If I'm completely confused with what I'm supposed to be doing, let me know.

**Answer** by DerDicke
·
Jan 16, 2018 at 08:42 PM

Old thread, but problem was never solved. Best solution for me was:

```
float ClampAngle(float angle, float from, float to)
{
// accepts e.g. -80, 80
if (angle < 0f) angle = 360 + angle;
if (angle > 180f) return Mathf.Max(angle, 360+from);
return Mathf.Min(angle, to);
}
void RotateInFrame()
{
if (!Input.GetMouseButton(1)) return; // RMB down
float mx = Input.GetAxis("Mouse X") * Time.deltaTime * rotSpeed;
float my = Input.GetAxis("Mouse Y") * Time.deltaTime * rotSpeed;
Vector3 rot = transform.rotation.eulerAngles + new Vector3(-my, mx, 0f); //use local if your char is not always oriented Vector3.up
rot.x = ClampAngle(rot.x, -60f, 60f);
transform.eulerAngles = rot;
}
```

Good luck!

Your code works perfectly, thank you!!!, simple and works

**Answer** by LFenix
·
May 09, 2017 at 05:57 AM

I know the question is old, but here is a quick formula for a angle clamped between -max & +max, with 0

**Answer** by Lairex59
·
May 02 at 12:33 PM

Here it's explained well https://www.youtube.com/watch?v=JeF0hoJWLz4

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

Is anyone able to fix my code? 3 Answers

clamp rotation on rigidbody 1 Answer

Edited mouselook script rotation clamp not working 0 Answers

Temporary Rotation 2 Answers

Rotation not observing limits 1 Answer