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

**Answer** by xoox
·
Apr 10, 2020 at 12:25 AM

@unit_nick This solution seems to work when facing one direction using -15 and 15 min/max angles but when my object changes direction and I try to adjust the min and max angles to 165 and 195 the object can't go above 180 degrees and gets locked to the min 165 angle. Do you know why this could be happening?

Because the values should never be outside the range of +/- 180. This is relative to the objects direction. Turning 195 defeats the purpose when it could just turn 165 in the other direction.

@unit_nick Then how do I clamp the rotation from -165 degrees to 165 degrees? It seems when I use -165, 165 for min/max angles it gives the object full range between -165 and 165 (330 degrees) but I'm trying to clamp the rotation to the same relativeRange (30 degrees) even when facing the other direction.

If you are changing direction then force an equivalent rotation of the object you want to clamp and then apply the clamp.

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