Mathf.Mathf.Approximately 不起作用 [英] Mathf.Mathf.Approximately does not work

查看:16
本文介绍了Mathf.Mathf.Approximately 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code for moving a fog sprite that passes trough the game and changes the background. The thing is that Mathf.Approximately does not return true. Any ideas why this is happening or how can I work around this without hard coding the position values.

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

推荐答案

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

I solved it by writing my own approximation, but I'm still interested in why Mathf.Approximately does not work. Here is the code that I wrote

private bool myApproximation(float a, float b, float tolerance)
    {
        return (Mathf.Abs(a - b) < tolerance);
    }

这篇关于Mathf.Mathf.Approximately 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆