- Home /
Detect GUI rotation degree
Hi, I have a small project with genre fishing. Have a problem on detect rotation. I have a GUI texture like a reel that can be rotated.
How can i make the length decrease based on each 15 degree the reel rotated? Here is my Rotate code in C#:
public class Rotate : MonoBehaviour {
public Texture2D texture = null;
public Vector2 size = new Vector2(0, 0);
private float angle = 0;
private Vector2 pos = new Vector2(0, 0);
private Rect rect;
private Vector2 pivot;
public static bool rotating = false;
public static bool rotating2 = false;
private float initialMouseAngle;
static float speed = 0.1f;
float factor = Screen.width/800;
void Start() {
UpdateSettings();
}
void UpdateSettings() {
pos = new Vector2(Screen.width-factor*700,Screen.height-factor*320+200);
rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, 150, 150);
//Debug.Log ("Rect="+rect);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
//Debug.Log ("Pivot ="+pivot);
}
void OnGUI() {
if (Application.isEditor){
UpdateSettings();
}
Vector2 guiMouse = Input.mousePosition;
guiMouse.y = Screen.height - guiMouse.y;
if (Input.GetMouseButtonDown(0) && rect.Contains(guiMouse)) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if (Input.GetMouseButton(0) && rotating) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle) * Mathf.Rad2Deg;
//GetComponent<Length>().Jarak(speed);
rotating2 = true;
rect.ToString();
}
else if (Input.GetMouseButtonUp(0)) {
rotating = false;
rotating2 = false;
}
Matrix4x4 matrixBackup = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
if(FishingGUI.waktu == true){
GUI.DrawTexture(rect, texture);
animation.Play("idle");
return;
}
GUI.matrix = matrixBackup;
}
}
I want to integrated with this want, so each 15 degree can decrease the length:
public class Length : MonoBehaviour {
[SerializeField]
float MaxLength = 100f;
float CurrentLength = 0f;
void Start () {
CurrentLength = MaxLength;
}
override public string ToString(){
return CurrentLength + "";
}
public void Jarak(float tarikans){
CurrentLength -= tarikans;
}
void Update(){
if(CurrentLength <= 0){
CurrentLength = 0;
}
}
}
Please anyone can help or any other solution? Thanks
There might be a way to implecate transform. into the script to make it more convinient unless c# does not use that.
thanks for replying, but i still do not get it. Can you give me some other clue? or maybe another solution beside detecting GUI degrees?
don't you already have the degree because you're using variables with RotateAroundPivot
You mean this? But this code just make it rotate, not explain anything about degrees. Anyone please help..
Vector2 v2T = ((Vector2)gui$$anonymous$$ouse - pivot);
angle = ($$anonymous$$athf.Atan2 (v2T.y, v2T.x) - initial$$anonymous$$ouseAngle) * $$anonymous$$athf.Rad2Deg;
$$anonymous$$aybe just grab an angle value when you start rotating. Check difference between that angle and an angle you update every frame. If it goes > or < 15 degrees make a change. If you release it at +- 6 store that and add that to your comparison next time.
Answer by sparkzbarca · Dec 31, 2013 at 01:59 PM
im feeling generous so here you go.
Quanternion StoredRotation;
Void Start()
{
StoredRotation = transform.rotation;
}
float Angle;
float Speed = .05f;
Void Update()
{
//anytime the GUI reel rotates it'll trigger this if statement
if(transform.rotation != StoredRotation)
{
//get the angle
Angle = Quanternion.Angle(transform.rotation, StoredRotation);
//modify length to be equal to the angle * the speed (which can be thought of as how
` `many
//meters to shorten the fishing line by per degree/radians (i'm actually not sure if
//quaternion.angle returns in degrees or radians) of change. So if you have a speed of 2
// it'll do 2 meters presumably per 1 degree/radian which is way high
//so id make it a small number.)
LineLength = angle * speed * time.deltatime;
}
}
oh whoops
LineLength = angle speed time.deltatime;
should be
LineLength += angle speed time.deltatime;
+=
also you'll need to toss in a bool to deter$$anonymous$$e if you should add or subtract
something like
int Line$$anonymous$$odifier;
//let out line
if(RotatingClockwise)
Line$$anonymous$$odifier = 1;
else
Line$$anonymous$$odifier = -1;
LineLength += Line$$anonymous$$odifier angle speed * time.deltatime;
i have tried many time with your code, and not work.I guess Quaternion does not work with GUI or 2D. So, any other solution sparkz?
Your answer
Follow this Question
Related Questions
Displaying Mathematical Equations 2 Answers
Detecting when the user starts and stop dragging a slider 1 Answer
i was wondering about gui hover... 3 Answers
Rotate 90 over time on mouseDown 2 Answers