Floating Point Error?

Hi! I’m making a game in unity 2D and I’m having a floating point issue. I have an object that I want to shift down .2f for every input of its button. but the inspector window when I test it, I notice a problem:
For reference, the code is:
if (Input.GetKeyUp(KeyCode.Keypad0)){
gameObject.transform.position -= new Vector3(0f, .2f, 0f);
}

So if the object starts at a Y of 6f, these are the Y positions:
6
5.8
5.6
5.400001
5.200001
5.000001
etc, then
0.600001
0.400001
0.200001
1.221895e-06

Why does it do this? It’s position in the game is, I assume, 0.000001, but then why does Unity think it is at the given space? I’m just frustrated and would love some help on what I can do to fix this or a better way to go about it that is more reliable. I need to keep track of its position and have things happen at certain coordinate values. Thanks in advance! :smiley:

The problem is floating point imprecision. To get around it you can use Mathf.Approximately(float1, float2);. Also, Unity basically does the same thing behind the scenes for Vectors when you use the default comparison for them so you’re covered there.