0

There is a UI button, when you click on it, the same button smoothly rotates 90 ° and the rotation stops, when you press the button again, the action is performed. But after the rotation is equal to 360 ° (0f by the code), the button does not rotate further. I'm at a loss why this is happening.

public GameObject Botton;
private float BottonRotationZ;
public void Rotate()
{
    if (Botton.transform.eulerAngles.z == 0f)
    {
        LeanTween.rotateZ(Botton,90f,0.5f);
    }

    else if (Botton.transform.eulerAngles.z == 90f)
    {
        LeanTween.rotateZ(Botton,180f,0.5f);
    }

    else if (Botton.transform.eulerAngles.z == 180f)
    {
        LeanTween.rotateZ(Botton,270f,0.5f);
    }
    
    else if (Botton.transform.eulerAngles.z == 270f)
    {
        LeanTween.rotateZ(Botton,0f,0.5f);
    }
} 
man
  • 13
  • 2
  • Print the angle to be sure what it is. Perhaps it only rotated to 359.5° – Weather Vane Oct 17 '20 at 16:03
  • @Weather Vane gives instead of 360: 1.001791E-05 – man Oct 17 '20 at 16:15
  • I was going to ask if `float` was totally accurate, until I noticed that it steps by `0.5` which is exactly representable by`float`. Perhaps it is because it works internally from mathematical **radians** and converts to and from human **degrees**. Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Oct 17 '20 at 16:18

2 Answers2

0

rounded the value

public GameObject Botton;
private float BottonRotationZ;
public void Rotate()
{
    int z = Convert.ToInt32(Botton.transform.eulerAngles.z);
    Debug.Log(z);

    if (z == 0)
    {
        LeanTween.rotateZ(Botton,90f,0.5f);
    }

    else if (z == 90)
    {
        LeanTween.rotateZ(Botton,180f,0.5f);
        
    }

    else if (z == 180)
    {
        LeanTween.rotateZ(Botton,270f,0.5f);
        
    }
    
    else if (z == 270)
    {
        LeanTween.rotateZ(Botton,0f,0.5f);
       
    }
}   
0

This seems to be a rounding issue. You check if your button is exactly at 0°, 90°, 180° or 270°, which is not necessarily always the case after a tween. One way to solve this is with the following function:

public void Rotate(float rotationAmount)
{
    if (!LeanTween.isTweening(this.gameObject))
    {
        LeanTween.rotateZ(this.gameObject, (this.gameObject.transform.eulerAngles.z + rotationAmount), 0.5f);
    }
}

This will rotate your button further from "current rotation" to "current rotation + rotationAmount", but only if it is not already tweening. Please note that you can only add values between bigger -180° and smaller +180° for each time you call the function.

Vivien Lynn
  • 345
  • 4
  • 14