0
\$\begingroup\$

I'm trying to do something very basic: "IF value of tranform.position.x equals 4.5 DO something"

Here is my code:

if(transform.position.x.Equals(4.5)){
        //(float)transform.position.x == (float)myGM.BottomRightCorner.x
        if(directionToMove.Equals(0.1f)){
            directionToMove = -0.1f;
            print("Direction To Move Changed");
        } else{
            directionToMove = 0.1f;
            print("Direction To Move Changed");
        }            
    }
    transform.Translate(new Vector2(directionToMove, 0));

I've tried if(transform.position.x == 4.5), if(transform.position.x == 4.5f, if(transform.position.x.Equals(4.5), if(transform.position.x.Equals(4.5f)and of course I've tried putting the 4.5 in a variable (constant and regular)... but no matter what, when transform.position.x has a value of 4.5... nothing happens. it completely skips the if statement. I've been programming for 5 hours so far today, so my brain might just have crashed... Just for the sake of it, I did try if(4.5 == 4.5) just to make sure I wasn't going crazy and somehow wrote the if statement incorrectly... but no, that worked just fine (Thank god...)

I'd greatly appreciate it if anyone can point out any clues as to what I'm doing wrong here.

p.s. I'm aware that my code is not the most efficient thing in the world... I'm trying to see what I can do by myself without following any tutorial. As for what the statement is meant to do... before the transform reaches 4.5, starting from 4.0, increase the transform.position by 0.1f. Once it does reach that, decrease the transform.position by 0.1f.

Basically, move to one side of the screen, then go back to the other side of the screen. Initially, there was a check for the other side, but I stripped this to bare-bones to try and figure it out.

p.s.2: As a side note, this function is called via an "InvokeRepeating()". I'm going to try calling the method in another way, just in case... if that works, I'll remove this post.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You should never test floating point values for equality. The reason is that floating point values always have a certain uncertainty.

There are some values which can be expressed in decimal notation, but can actually not be expressed as floating point values. The reason is that floating point values are binary and not decimal internally. The value for 0.8f is invalid, for example. So the result of 0.7f + 0.1f will be "rounded up" to 0.8000001f. When you keep adding to and subtracting from floating point values, like you are doing here, such rounding errors accumulate.

For that reason, you should always check if floating point values are within a range which you consider "close enough" for your purpose. Unity has a utility method for that: Mathf.Approximately(transform.position.x, 4.5f). But this method is still rather strict, because it only considers the smallest possible difference between two floating point values as "close enough". Sometimes you might have to use a larger tolerance factor. For example, when you consider 0.1f "close enough", you can use if(Mathf.Abs(transform.position.x - 4.5f) <= 0.1f).

For more information on common mistakes when dealing with floating point values (applicable to almost any programming language), check out the famous website What Every Programmer Should Know About Floating-Point Arithmetic.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! That sorted it instantly... I'll read through that website now. I didn't consider the possibility of "The value simply is never 4.5" since I knew I was incrementing by 0.1 every time- Unity printing the exact value of "4.1", "4.2" etc pushed me further away from that possibility too. I probably would have spent a good couple hours trying to figure this out... \$\endgroup\$ Dec 26, 2020 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .