Restrict Rotation

I have this script that rotates around an object acccording to where the mouse point is. But I want to restrict it with mathf.clamp but I cant seam to get it to work. What am I doing wrong?

 var unitScript: ArmyBox = targetA.GetComponent(ArmyBox);

 


mouse_pos = Input.mousePosition;

object_pos = Camera.main.WorldToScreenPoint(targetA.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;

targetA.rotation = Quaternion.Euler(Vector3(0, -angle, 0));
  
targetA.rotation.y = Mathf.Clamp(targetA.rotation.y, -45, 45);
print(targetA.rotation);

Transform.rotation is a 4D quaternion that doesn’t use degrees and the axes don’t directly correspond to euler angles. As the docs say, don’t modify it unless you know quaternions inside out. Take a look at the default MouseLook script.

–Eric

This is what I finally got.

mouse_pos = Input.mousePosition;

object_pos = Camera.main.WorldToScreenPoint(targetA.position);//get mouse position
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;

targetA.rotation  = Quaternion.Euler(Vector3(0, -angle, 0));//set objects rotation too it



if(targetA.eulerAngles.y > unitScript.Units.turningSpeed   targetA.eulerAngles.y < (360-unitScript.Units.turningSpeed)){
targetA.eulerAngles.y =unitScript.Units.turningSpeed;
}

Ok, so it works but im having a problem. I wnat to rotate it multiple times but in an ammont of degrees. So when I rotate it once it rotates to that number, but then I cant rotate it any farther.