- Home /
Limit GUI Rotation?
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;
}
}
Answer by Immanuel-Scholz · Jul 02, 2013 at 01:16 PM
MH... Idea: In line 36, instead of using the "correct" gui position, just clamp the position to the upper screen.
guiMouse.y = 0;
Hmmm, that doesn't seem to help either. I have found out, that it's because the angle is not going from 0-360 but it's actually changing the angle to different values. It can be negative and positive numbers and it seems to be changing whenever the mouse/gui has moved 270 degrees. I don't know what can be causing it.
Thanks, Andreas.
because the angle is not going from 0-360
Using Atan2, I would have expected the angle goes from -PI/2 to PI/2, (-180 to 180).
So yea, your trail is good. You probably have some hickup when subtracting the angles from each other and you need somewhere an "`if (angle < 0) angle += 360`".
Can't figure out exactly where, though. These angle stuff always gives me headaches :( ;)
Thank you :-) I have tried for hours of hours now, but I have still not found out how to do it.. If you have more ideas about what could solve it, I would be really happy :-)
Thank you :) I have figured it out now by converting the angle value to a 0-360 degress angle with this:
angle = -angle; angle = 360 - angle;
It works perfectly now :)
Answer by Jamora · Jul 03, 2013 at 02:45 AM
If the problem is that the mouse going under the pivot point's y coordinate causes unintended behavior. Try something like this:
if(guimouse.y<pivot.y)
angle = Mathf.Clamp (angle, -70, 70);
Note that Input.mouseposition has an inverted y-coordinate; bottom = 0, while OnGUI uses top = 0. But since you have it working, you've already accounted for this, somehow.
Thank's for your answer, Jamora. I have figured it out now by converting the angle value to a 0-360 degress angle with this:
angle = -angle; angle = 360 - angle;
It works perfectly now :)
Hi Andreas, have you solved this problem? I have the same problem, trying for hours.. If you have found a solution please show how to limit the angle if mouse is going under pivot y
Hi $$anonymous$$ilot. I looked into my old scripts and it seems like i added this code before line 54 (angle = $$anonymous$$athf.Clamp (angle, -70, 70);):
if(angle > 360) {
angle = 0;
}
if(angle < -80) {
angle = -angle;
angle = 360 - angle;
}
Let me know if it works :)