Unity3d 如果这句话即使是真的也不起作用,那就团结起来

Unity3d 如果这句话即使是真的也不起作用,那就团结起来,unity3d,Unity3d,当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;

当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");
        }
    }
}
public class WhyDosntThisWork:monobhavior
{
公共布尔北=假;
公共布尔南=假;
公共布尔西=假;
公共布尔东=假;
公共bool=false;
公共浮动x=0;
公共浮动y=0;
公共bool IsRotating=false;
公共矢量3播放器;
公共浮动if0trueifnotfalse=0;
void Start()
{
//Player=transform.up;//在这里也尝试过,仍然没有工作
}
无效更新()
{
Player=transform.up;
y=Input.GetAxisRaw(“垂直”);//按箭头键
x=Input.GetAxisRaw(“水平”);//按箭头键
打印(“y=”+y);
打印(“x=”+x);
如果(y==0)
{
WerEareWebLooking();//运行函数应该工作吗???
打印(“我们正在运行脚本”);
}
如果(y>0)
{
打印(“我们展示了player.x现在是1”);
transform.eulerAngles=newvector3(0,0,-90);//这将player.x从-1更改为1
}
if(y<0)
{
打印(“我们将玩家的x降为-1”);
//WereAreWeLooking();
transform.eulerAngles=newvector3(0,0,90);//这将player.x从1更改为-1
}
}
void werearewewlooking()
{
打印(“嗨,我们正在检查bools播放器。x是“+Player.x+”,那么我们为什么不更改bool”);
if(Player.x==-1)//即使play.x是-1,它也不会运行
{   
打印(“我们要走了”);
北=真;
南=假;
东=假;
西=假;
}
else if(Player.x==1)
{
打印(“我们正在下降”);
南方=正确;
北=假;
东=假;
西=假;
}
else if(Player.z==1)
{
印刷(“我们向东走”);
东=真;
南=假;
北=假;
西=假;
}
else if(Player.z==-1)
{
印刷(“我们要向西走”);
西=真;
东=假;
南=假;
北=假;
}
打印(“感谢检查完成”);
吉米=对;
if(if0trueifnotfalse==1)//如果我在inspector中更改值,则此操作有效
{
吉米=假;
印刷品(“吉米是1”);
}
}
}

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

如果计算出该值,则该值很有可能不完全为1或-1。它将是
1.0000000001
0.9999999999
(例如)

这意味着您的测试如下所示:

if (Player.x == -1)
永远都会失败

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

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

即使您使用
double
,您仍然会得到这些舍入错误-尽管大大减少了。可能的情况是,无论你用什么方法来检查值,都会执行一些舍入操作,因此看起来值是-1或1。

您好,谢谢您的回答,这在检查员中是有意义的,它必须舍入它们,这让我很惊讶。谢谢,我稍后会测试它,因为不是在我的计算机上,但再次感谢这让我很兴奋。谢谢
code'if(Mathf.about(Player.x,-1.0f))
是一个完美的答案,谢谢。我如何将您的答案标记为正确答案再次感谢
if (Mathf.Approximately(Player.x, -1.0f))