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

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

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.

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;