What exactly do you want to limit? This script is trying to limit its owner object’s rotation around itself, not around the target object - is this what you actually want to do? Anyway, it would be better to find the rotation angle by other more predictable means - reading eulerAngles is tricky and usually very disappointing: weird values may be returned at certain ranges.
If the object only rotates horizontally (about the Y axis), you can use Vector3.Angle like this:
var angle = Vector3.Angle(Vector3.forward, transform.forward);
The angle returned is positive and in the range 0…180, thus you won’t know if it’s to the left or to the right. If you need to know the full angle (from -180 to 180), use this:
var angle = Vector3.Angle(Vector3.forward, transform.forward);
if (Vector3.Cross(Vector3.forward, transform.forward).y < 0) angle = -angle;
When turned to the left of the world forward direction, a negative angle will be returned. If you want to know the rotation angle relative to the initial object rotation, just save its transform.forward direction at Start and use it in place of Vector3.forward.
Once you have a reliable angle information, you can limit its value and set it back using eulerAngles (different from reading, setting eulerAngles is reliable!):
var angle = Vector3.Angle(Vector3.forward, transform.forward);
if (Vector3.Cross(Vector3.forward, transform.forward).y < 0) angle = -angle;
angle = Mathf.Clamp(angle, minAngle, maxAngle)
transform.eulerAngles = Vector3(0, angle, 0);
But this only clamps the rotation around itself - if you want to clamp RotateAround to a certain angle range, things are way more complex because the object isn’t only rotated: it’s also translated in a circular fashion. In order to avoid complicated calculations, it’s better to check and clamp the angle before using RotateAround:
function Update(){
var dir = transform.position - target.position; // find current direction
var angle = Vector3.Angle(Vector3.forward, dir); // find current angle
if (Vector3.Cross(Vector3.forward, dir).y < 0) angle = -angle;
// define rotation angle according to tourchLeft/tourchRight:
var rotAng: float;
if (playerScript.tourchLeft){
rotAng = speed * degrees;
} else {
rotAng = speeds * degrees;
}
// calculate the clamped angle after rotation:
var newAngle = Mathf.Clamp(angle + rotAng, minAngle, maxAngle);
// find how much you can rotate without violating limits:
rotAngle = newAngle - angle;
// rotate it:
RotateAround(target.position, Vector3.up, rotAngle);
}
NOTE: The last code fully clamps the RotateAround range to the given limits. The other code snippets above are intended to limit the object rotation around itself - use the code that’s suitable to your case, not both!