1

Why isn't the if statements getting read when the value of play.x is definitely 1 or -1?

Any help would be great thanks if there is anything else you need to know I'll do my best to explain it.

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");
        }
    }
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
user2164327
  • 283
  • 1
  • 2
  • 13

1 Answers1

3

You are comparing floating point numbers via an equality operator.

If the value is computed then there's a very good chance that the value won't be exactly 1 or -1. It'll be 1.0000000001 or 0.9999999999 (for example).

This means that your tests like this:

if (Player.x == -1)

will always fail.

You need to introduce rounding into your test:

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

This will check that Player.x is equal to -1 to 6 decimal places, so -0.999999 and -1.000001 will pass the test. You might need to tweak the epsilon value to get a stable solution for your data.

As you are using Unity you can use their built in functionality Mathf.Approximately:

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

Even if you use double you'll still get these rounding errors - though much reduced. It's probably the case that whatever you're using to inspect the values is performing some rounding so it looks like the values are -1 or 1.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Hi thanks for your reply that makes sense in the inspector it must round them up that was throwing me thanks I’ll test it later as not at my computer but thanks again this was driving me mad – user2164327 Feb 07 '19 at 14:04
  • hi thanks the `code' if (Mathf.Approximately(Player.x, -1.0f)) ` was the perfect answer thanks how do i mark yours as correct answer thanks again – user2164327 Feb 07 '19 at 17:36