【问题标题】:with LeanTween GameObject does not rotate more than 360 °使用 LeanTween 游戏对象旋转不超过 360°
【发布时间】:2020-12-14 23:40:24
【问题描述】:

有一个UI按钮,当你点击它时,同一个按钮平滑旋转90°并停止旋转,当你再次按下按钮时,执行动作。 但是在旋转等于 360°(代码为 0f)后,按钮不会进一步旋转。我不知道为什么会这样。

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);
    }
} 

【问题讨论】:

  • 这看起来不像 C...
  • 打印角度以确定它是什么。也许它只旋转到 359.5°
  • @Weather Vane 给出而不是 360:1.001791E-05
  • 我正要问float 是否完全准确,直到我注意到它的步长为0.5,而float 完全可以表示。也许是因为它在内部使用数学 弧度 并转换为人类 。请参阅Is floating point math broken?Why Are Floating Point Numbers Inaccurate?

标签: unity3d


【解决方案1】:

四舍五入

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);
       
    }
}   

【讨论】:

    【解决方案2】:

    这似乎是一个四舍五入的问题。您检查您的按钮是否正好在 0°、90°、180° 或 270°,在补间之后不一定总是如此。 解决此问题的一种方法是使用以下函数:

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

    这会将您的按钮从“当前旋转”进一步旋转到“当前旋转 + rotationAmount”,但前提是它还没有补间。 请注意,每次调用该函数时,您只能在较大的 -180° 和较小的 +180° 之间添加值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多