RotateAround clamp not working as expected

Hey all;

I’m rotation an object 180 degrees around the player when the use presses either left; or right.

This works; though when the object is at maxAngle; it keeps rotating between min and maxAngle very fast. and doesn’t stay clamped.

for example if minAngle = 180 and maxAngle= 360 I will get the above problem. however if maxAngle = 350; these isn’t a problem… and it will clamp between the two.

Script:

var degrees = 20;
var target : Transform;
var speed = 0.5;
var speeds = -0.5;
var playerScript : playerScript;

// Define Limits of Rotation:
static var minAngle : float = 180;
static var maxAngle : float = 350;

function Start (){
	var someScript : playerScript = gameObject.GetComponent("playerScript");

}

function Update () {

	if (playerScript.tourchLeft == true){
	transform.RotateAround (target.position, Vector3.up, degrees * speed);
    transform.eulerAngles = Vector3(0,Mathf.Clamp(transform.eulerAngles.y, minAngle, maxAngle),0);
     }

     if (playerScript.tourchRight == true){
	    transform.RotateAround (target.position, Vector3.up, degrees * speeds);
     transform.eulerAngles = Vector3(0,Mathf.Clamp(transform.eulerAngles.y, minAngle, maxAngle),0);
     }

 }

I don’t get it; hence the question.
Thank you.

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!