Hello, I have this script which rotates a gui (In my case, a steering wheel). I’am trying to limit the angle from -70 degress to 70 degress. I have done that by using Mathf.Clamp, which works perfectly. The only problem is, that when my mouse’s Y position comes under the pivotPoint’s Y position, the angle = 70. Here’s the code: (Just create an empty C# script, attach it to a game object, assign a texture and try to rotate the gui)
Thanks, Andreas
public Texture2D texture = null;
public Vector2 GUIsize;
public Vector2 v2T;
public float angle = 0;
private Vector2 pos = new Vector2(0,0);
private Rect rect;
private Vector2 pivot;
private bool rotating = false;
private bool drawAngle = true;
public float initialMouseAngle;
void Start() {
UpdateSettings();
}
void UpdateSettings() {
GUIsize = new Vector2((128 * Screen.width / 1000) + (128 * Screen.height / 1000) , (128 * Screen.width / 1000) + (128 * Screen.height / 1000));
pos = new Vector2(Screen.width, Screen.height);
rect = new Rect(pos.x - GUIsize.x, pos.y - GUIsize.y , GUIsize.x, GUIsize.y);
Debug.Log ("Rect="+rect);
pivot = new Vector2(rect.xMin + rect.width / 2, rect.yMin + rect.height / 2);
}
void OnGUI() {
if (Application.isEditor) {
UpdateSettings();
}
Vector2 guiMouse = Input.mousePosition;
guiMouse.y = Screen.height - guiMouse.y;
if (Input.GetMouseButtonDown(0) && rect.Contains(guiMouse)) {
v2T = ((Vector2)guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if (Input.GetMouseButton(0) & rotating) {
v2T = ((Vector2)guiMouse - pivot);
angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle) * Mathf.Rad2Deg;
}
else if (Input.GetMouseButtonUp(0)) {
rotating = false;
}
angle = Mathf.Clamp (angle, -70, 70);
if(drawAngle == true) {
Matrix4x4 matrixBackup = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, texture);
GUI.matrix = matrixBackup;
}
}