【问题标题】:unity if statement wont work even when true [duplicate]即使是真的,如果声明也不会起作用[重复]
【发布时间】:2019-02-07 13:29:57
【问题描述】:

当 play.x 的值肯定是 1 或 -1 时,为什么不读取 if 语句?

如果您还有什么需要知道的,任何帮助都将非常感谢,我会尽力解释。

public class WhyDosntThisWork : MonoBehaviour
{
    public bool North = false;
    public bool South = false;
    public bool West = false;
    public bool East = false;
    public bool jimmy = false;
    public float x = 0;
    public float y = 0;
    public bool IsRotating = false;
    public Vector3 Player;
    public float if0trueifnotfalse = 0;

    void Start()
    {
        //Player = transform.up;// tryed here aswell still no work
    }

    void Update()
    {
        Player = transform.up;
        y = Input.GetAxisRaw("Vertical");// press arrowkey 
        x = Input.GetAxisRaw("Horizontal");// press arrowkey 
        print(" y =  " + y);
        print(" x =  " + x);
        if (y == 0)
        {
            WereAreWeLooking();// run function  should work???
            print("we are Running the Script");
        }
        if (y > 0)
        {
            print("We Presed up player.x is now 1");
            transform.eulerAngles = new Vector3(0,0,-90); // this changes player.x from -1 to 1
        }
        if (y < 0)
        {
            print("We Presed down player.x is now -1");
           // WereAreWeLooking();
            transform.eulerAngles = new Vector3(0,0,90); //this changes player.x from 1 to -1

        }
    }

    void WereAreWeLooking()
    {
        print("HI we are checking for bools Player.x  IS " + Player.x + " so why dont we change the bool");
        if (Player.x == -1)// this never runs even tho play.x is -1
        {   
            print("We Are GoingUp");
            North = true;
            South = false;
            East = false;
            West = false;
        }
        else if (Player.x == 1)
        {
            print("We Are GoingDown");
            South = true;
            North = false;
            East = false;
            West = false;
        }
        else if (Player.z == 1)
        {
            print("We Are going East");
            East = true;
            South = false;
            North = false;
            West = false;
        }
        else if (Player.z == -1)
        {
            print("We Aregoing west");
            West = true;
            East = false;
            South = false;
            North = false;
        }
        print("Thanks Checking done");
        jimmy = true;

        if (if0trueifnotfalse == 1)// this works if i change the value in the inspector
        {
            jimmy = false;
            print("jimmy is 1");
        }
    }
}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您正在通过相等运算符比较浮点数。

    如果该值是计算出来的,那么该值很可能不会正好是 1 或 -1。它将是 1.00000000010.9999999999(例如)。

    这意味着你的测试是这样的:

    if (Player.x == -1)
    

    总会失败。

    您需要在测试中引入舍入:

    if (Player.x + 1 < 10e-6)
    

    这将检查Player.x 是否等于-1 到6 位小数,因此-0.999999-1.000001 将通过测试。您可能需要调整 epsilon 值以获得稳定的数据解决方案。

    当您使用 Unity 时,您可以使用它们的内置功能 Mathf.Approximately:

    if (Mathf.Approximately(Player.x, -1.0f))
    

    即使您使用double,您仍然会得到这些舍入误差 - 尽管减少了很多。可能是您用来检查值的任何东西都在执行一些舍入,所以看起来值是 -1 或 1。

    【讨论】:

    • 您好,感谢您的回复,这在检查员中是有道理的,它必须将它们围起来,这让我很失望,谢谢我稍后会在我的电脑上测试它,但再次感谢这让我发疯跨度>
    • 嗨谢谢code' if (Mathf.Approximately(Player.x, -1.0f)) 是完美的答案谢谢我如何将你的标记为正确答案再次谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 2019-05-26
    • 2019-08-21
    • 2020-11-08
    • 2021-07-02
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多