- Home /
Rotate 2D Texture negative positive degrees touch
Hello,
I am working on a kind of wheel which you can rotate 360 degrees with your fingers. The positive degrees are working nicely. But the problem is, how can i make it, so when your finger goes to the left, the degrees goes negative?
Thanks in advance, Flux
Code:
public var texture : Texture2D = null;
public var size : Vector2 = new Vector2(128, 128);
private var angle : float = 0;
private var pos : Vector2 = new Vector2(0, 0);
private var rect : Rect;
private var pivot : Vector2;
private var rotating : boolean = false;
private var initialMouseAngle : float;
function Start() {
UpdateSettings();
}
function UpdateSettings() {
pos = new Vector2(180, Screen.height - texture.height + 350);
rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}
function OnGUI() {
if (Application.isEditor) { UpdateSettings(); }
if(Input.touchCount > 0) {
var touch : Touch = Input.GetTouch(0);
var guiMouse : Vector2 = Vector2(touch.position.x, touch.position.y);
guiMouse.y = Screen.height - guiMouse.y;
if ((touch.phase == TouchPhase.Began) && rect.Contains(guiMouse)) {
var v2T : Vector2 = (guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if ((touch.phase == TouchPhase.Moved) & rotating) {
var v2T2 : Vector2 = (guiMouse - pivot);
angle = (Mathf.Atan2 (v2T2.y, v2T2.x) - initialMouseAngle) * Mathf.Rad2Deg;
if(angle < 0) {
angle += 360;
Debug.Log(angle);
}
}
else if ((touch.phase == TouchPhase.Ended)) {
rotating = false;
angle = 0;
}
}
angle = Mathf.Clamp(angle, -360, 360);
GUI.Label(Rect(100, 30, 100, 30), angle.ToString());
var matrixBackup : Matrix4x4 = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, texture);
GUI.matrix = matrixBackup;
}
Is the issue that the display isn't rotating, or that the touch isn't showing a negative rotation?
The issue is that it doesn't show a negative rotation value!
Just to make sure it isn't something silly that I might do and to understand the question...
This ensures it will never be negative. if(angle < 0) { angle += 360;
Debug.Log(angle);
That's weird. The only thing I can see is that it says that OnGui can be called multiple times per frame. Can you move that whole thing to the Update() method ins$$anonymous$$d and see if that helps? Aside from that, I'm out of ideas.
Answer by Dolkar · Jun 22, 2013 at 10:53 PM
Try setting the angle to angle % 360. The reported number will always be between 0 and 360, but the wheel should move as expected.
Thanks for your response, but that also didn't work! The problem it's the same, when i hit the -90 degrees, it goes to 270 degrees.
Well, the object is a kind of wheel. When i rotate my finger to the left around the wheel, the degrees goes negative until -90 degrees. Then the degrees goes positive, 270 degrees. It works fine when i rotate my finger to the right around the wheel, the degrees goes then to 360 degrees positive. So, the negative degrees are not working properly, i want that the negative degrees goes to -360 degrees.
And when it reaches these boundaries you want the wheel to stop or wrap around?
Answer by RakeshSingh · Jul 20, 2013 at 02:55 PM
remove this form ur code......... if(angle < 0) { angle += 360;