- Home /

# How to clamp rotation between negative and positive value

I am trying to clamp my rotation between -20 and 45 but i tried somethings and i still couldnt do it.

I even tried to use only positive values like 330 or something it has a problem with the maximum value being 45 and then it freaks out. I have tried some stuff but im not getting there this is what i have got now. Any help is appreciated thanks.

```
Vector3 clampedTurret = new Vector3(turretToClamp.turret.transform.eulerAngles.x, turretToClamp.turret.transform.eulerAngles.y, turretToClamp.turret.transform.eulerAngles.z);
float zValue = clampedTurret.z;
if (zValue - 360 < turretToClamp.minAngle)
{
zValue = turretToClamp.minAngle + 360;
chng = true;
}
if (zValue > turretToClamp.maxAngle)
{
zValue = turretToClamp.maxAngle;
chng = true;
}
if (zValue < 0)
zValue = zValue + 360;
if (zValue > 360)
zValue = 0;
clampedTurret.z = zValue;
```

**Answer** by unit_nick
·
Sep 20, 2017 at 02:00 PM

```
public float minAngle = -20;
public float maxAngle = 40;
private void Update()
{
// get relative range +/-
float relRange = (maxAngle - minAngle) / 2f;
// calculate offset
float offset = maxAngle - relRange;
// convert to a relative value
Vector3 angles = turretToClamp.turret.transform.eulerAngles;
float z = ((angles.z + 540) % 360) - 180 - offset;
// if outside range
if (Mathf.Abs(z) > relRange)
{
angles.z = relRange * Mathf.Sign(z) + offset;
turretToClamp.turret.transform.eulerAngles = angles;
}
}
```

Hey thanks for the answer but could u explain a little bit more about what you have done?

Sure.

Relative range is half the whole range. This is so we can convert the rotation to a relative +/- which means we only have to test for 1 maximum value

Because the ranges aren't equal on either side an offset is required to make the relative values equal

The turret angle z value is then converted to its relative offset value

If the absolute (positive) value of this relative offset value of z is greater than half the whole range then it must be outside either the min or max value. In which case we limit it to half the whole range. Then relRange is multiplied by the sign of z so that relRange is now a negative value if z is negative, or a positive value if z is positive. The the offset that was removed is put back so as to account for the ranges not being equal.

Lastly the original angle with the modified z axis is returned to its transform so as to update its rotation.

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

Flip over an object (smooth transition) 3 Answers

Reverse object position order 1 Answer

Problem with Mathf.Clamp and Mouse Follow Script 1 Answer