Unity到Android-简单的if语句不起作用 [英] Unity to Android - Simple if statement not working
问题描述
只需在Unity编辑器中测试我的游戏即可,一切正常:
Just tested my game in Unity editor and all works fine:
void Update () {
if (transform.position.y < -3f) {
Debug.Log ("game over!");
prepareGame ();
}
}
如果在y小于-3的情况下执行if,则一切正常,但是...
All works fine the if is executed when y is below -3 but...
当我导出游戏并在android上进行测试时,if语句永远不会 被叫!
When I export the game and test on android, the if statement never gets called!
我调试了droid版本,值Y小于-3,但仍然 如果永远都不会被调用并且游戏永远不会结束...为什么?
I debuged the droid version and the value Y is BELOW -3 but still, the if never gets called and the game never ends...why?
推荐答案
最佳做法是不比较浮点值.
It's best practices not to compare float values.
解决方案:
1)使用Mathf.aboutly(浮点a,浮点b)链接
1) Use Mathf.Approximately(float a, float b) link
2)使用Mathf.RoundToInt(float f)链接
2) Use Mathf.RoundToInt(float f) link
3)使用触发器.这可能超出您所需的解决方案
3) Use triggers. This might be more than you need for this solution
我没有足够的代表来链接#3,而是查找了Collider.OnTriggerEnter(Collider)
I don't have enough rep to link #3 but lookup Collider.OnTriggerEnter(Collider)
这篇关于Unity到Android-简单的if语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!