Mathf.Mathf.Approximately 无效
Mathf.Mathf.Approximately does not work
我有以下代码用于移动经过游戏并更改背景的雾精灵。问题是 Mathf.Approximately 并不 return 正确。关于为什么会发生这种情况的任何想法,或者我如何在不对位置值进行硬编码的情况下解决这个问题。
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 不起作用。这是我写的代码
private bool myApproximation(float a, float b, float tolerance)
{
return (Mathf.Abs(a - b) < tolerance);
}
我认为问题在于 Mathf.Approximately
不 return 正确,因为实际上调用此方法时两个值并不相同。
这是因为你在Update
方法中调用了Mathf.Approximately
,这个方法每隔Time.deltaTime
调用一次。因此,如果恰好在这些时刻值不相同,Mathf.Approximately
将不会 return 为真。
这就像您正在看某人数数,他会在您眨眼时显示您期望的数字。你不可能知道这个数字已经被统计过了
我有以下代码用于移动经过游戏并更改背景的雾精灵。问题是 Mathf.Approximately 并不 return 正确。关于为什么会发生这种情况的任何想法,或者我如何在不对位置值进行硬编码的情况下解决这个问题。
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 不起作用。这是我写的代码
private bool myApproximation(float a, float b, float tolerance)
{
return (Mathf.Abs(a - b) < tolerance);
}
我认为问题在于 Mathf.Approximately
不 return 正确,因为实际上调用此方法时两个值并不相同。
这是因为你在Update
方法中调用了Mathf.Approximately
,这个方法每隔Time.deltaTime
调用一次。因此,如果恰好在这些时刻值不相同,Mathf.Approximately
将不会 return 为真。
这就像您正在看某人数数,他会在您眨眼时显示您期望的数字。你不可能知道这个数字已经被统计过了