- Home /
How to normalize rotation?
So on double mouse click my parent object rotates clockwise by 90 degrees, so it should be 0, 90, 180, 270 (0 or 360). But reality are different sometimes a have some floating number egz(90.000001), why so, gow to normalize it?
private int clickCounter = 0;
float targetX = 90.0f;
float timer = 0.5f;
void OnMouseDown(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.parent.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
gameObject.renderer.sortingOrder = 2;
if (Input.GetMouseButton (0)) {
mouseClickStart = true;
Invoke("checkMouseDoubleClick",timer);
clickCounter++;
if (clickCounter == 2) {
//gameObject.p.transform.rotation = Quaternion.Euler (0,0,targetX);
gameObject.transform.parent.rotation = Quaternion.Euler(0,0, targetX);
targetX = targetX-90.0f;
}
}
}
private void checkMouseDoubleClick()
{
if(clickCounter > 1){
Debug.Log("Double Clickedd");
}else{
Debug.Log("Single Clicked");
}
mouseClickStart = false;
clickCounter = 0;
}
Answer by RudyTheDev · Dec 05, 2014 at 01:16 AM
That's a limitation of floating point precision variables. Numbers in decimal system don't necessarily match with their binary representation. In Unity, it is very prevalent (or rather, noticeable because it shows them all over the place). There isn't anything you can do directly about it. You can manually round them off if you need whole numbers. But so long as they are part of GameObject/calculations/physics/change, they can eventually get jiggled.
Edit: Generally, floats are compared with Mathf.Abs(a - b) < epsilon
, where epsilon
is your chosen precision/tolerance (this is quick & dirty approach and there are downsides to it if you google around about this topic). You can use Unity's Mathf.Approximately()
as tanoshimi points out below.
but i need to combine two and more objects into one, when they have equal rotation, how to do that?
You don't ever compare exact equality of two floating point numbers - you compare whether they are the same within a certain tolerance using $$anonymous$$athf.Approximately
Thank you, but $$anonymous$$athf.Approximately, work only in first time of obj rotation if rotate obj in 720 degrees it is not "0", but a -2.003582e-05, and $$anonymous$$athf.Approximately doesn't work.
UPDATE, fixed problem with: if ($$anonymous$$athf.Abs(figure[0].parent.rotation.z - figure[2].parent.rotation.z) <= threshold)
Your answer
Follow this Question
Related Questions
magnitude normalize help 1 Answer
Normalizing a vector 1 Answer
direct and reverse proportion between variables 2 Answers
Getting the radius of a square 2 Answers
Vector2 Normalize ? 3 Answers