- Home /

# Limit gameobject rotation to -480 and 480 degrees?

I can't clamp the game object rotation to -480 and 480 degrees?

This is the video for my problem: Touch rotate object

You can find the code here: Touch rotate object code

I've tried to to add the transform.localEulerAngles.z to a variable, but the angle is wrong.

```
testAngle += transform.localEulerAngles.z;
```

The problem here is, that I only get numbers from 0 - 360 from the game object. I must somehow calculate the angles or can I somehow get transform rotation greater 360?

Not sure I understand... Greater than 360 loops around to 0 again. That's just how degrees work. In terms of orientation, there is zero difference between angles 10 and 370. You'd have to track and store the rotation yourself to put those limits on it.

And this my problem. I don't know how to do that? $$anonymous$$aybe I searched the wrong keywords on google. Can you please give some links or tips if you have some.

**Answer** by Eno-Khaon
·
May 03, 2017 at 01:05 AM

It's slightly more involved to handle larger angles, but not ***THAT*** bad. Rather than worrying about absolute angles, you can instead handle relative angles.

Using the linked script as a template, I made a few adjustments:

```
using UnityEngine;
public class ClickDragRotate : MonoBehaviour
{
public float minAngle = -480;
public float maxAngle = 480;
float angle = 0;
float lastAngle = 0;
void OnMouseDown()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
lastAngle = Mathf.Atan2(pos.y, pos.x);
}
void OnMouseDrag()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
// 2D rotation matrix to undo the current rotation
// [cos(T) -sin(T)]
// [sin(T) cos(T)]
Vector3 rotatedPos = new Vector3(pos.x * Mathf.Cos(-lastAngle) - pos.y * Mathf.Sin(-lastAngle), pos.x * Mathf.Sin(-lastAngle) + pos.y * Mathf.Cos(-lastAngle), 0);
float currentAngle = Mathf.Atan2(rotatedPos.y, rotatedPos.x);
angle += currentAngle * Mathf.Rad2Deg;
lastAngle = Mathf.Atan2(pos.y, pos.x);
angle = Mathf.Clamp(angle, minAngle, maxAngle);
transform.rotation = Quaternion.AngleAxis(angle, transform.forward);
}
}
```

I save the last frame's angle, then use that to rotate the current frame's position vector backward. At that point, *Atan2()* can be used safely without (usually) worrying about the *-180/180 degree* boundary. Once I have that new angle for the current frame, I simply add that to the current total angle and clamp it between the defined *minimum* and *maximum* angles.

**Answer** by toddisarockstar
·
May 03, 2017 at 01:10 AM

in Eularangles, Angles are represented between zero and 360. In other words, if there where an angle of 480 it would be the same as 120. It's just like a clock's hand passing around in cirlces past 12.

you can use Quaterntions to represent angles also. and they will except numbers that are higher than 360 to save "loop around" info but they are more complicated to learn than eular angles. I reccomend you stick to eular angles though.

if you give a little more info on what you are trying to do We can pry help you fix your problem.

anyways, you can always store rotations above 360 like this if you really need to know for whatever reason

```
transform.eulerAngles = Vector3.zero;
int myspins=0;
float addtoangle=480;
addtoangle=addtoangle+transform.eulerAngles.z;
while (addtoangle>360f) {myspins++;
addtoangle += -360f;
}
transform.eulerAngles += new Vector3 (0, 0, addtoangle);
float f = myspins * 360f;
f = f + transform.eulerAngles.z;
print ("the rotation you are asking for is : " + f);
print ("but the real rotation your pointing is : " + transform.eulerAngles.z);
print ("i spun around " + myspins + " times get here. as long as i know this i can always convert ");
```

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.