Mathf.Mathf.Approximately 不起作用

Uri*_*pov 0 c# unity-game-engine

我有以下代码用于移动穿过游戏并改变背景的雾精灵。问题是 Mathf.Approximately 不会返回 true。任何想法为什么会发生这种情况,或者我如何解决这个问题而不需要对位置值进行硬编码。

 fog.transform.position = new Vector3(Mathf.Lerp(fog.transform.position.x,gameObject.transform.position.x,transitionTime * Time.deltaTime),
                                                                                                                 gameObject.transform.position.y, 0f);

        if (Mathf.Approximately(fog.transform.position.x, backGround.transform.position.x))
        {
            index++;
            currentBackround.sprite = enviroments[index];
            Debug.Log(index);
        }
        if (Mathf.Approximately(fog.transform.position.x, gameObject.transform.position.x))
        {
            fog.transform.position = startPos;
        }
Run Code Online (Sandbox Code Playgroud)

Uri*_*pov 5

我通过编写自己的近似值解决了这个问题,但我仍然对为什么 Mathf.Approximately 不起作用感兴趣。这是我写的代码

private bool myApproximation(float a, float b, float tolerance)
    {
        return (Mathf.Abs(a - b) < tolerance);
    }
Run Code Online (Sandbox Code Playgroud)

  • 这就是 Unity 实现它的方式,以防任何人感兴趣 `public static bool approximation (float a, float b) { return Mathf.Abs (b - a) &lt; Mathf.Max (1E-06f * Mathf.Max (Mathf.Abs) (a),Mathf.Abs (b)),1.121039E-44f);}` (2认同)