When using == on a float it will rarely be equal. Rounding errors and the such cause it so its -5.9999999999 then -6.00000001 so it will not equal exactly -6.
Edit:
If you use Debug.Log on the float it will round it for display so it looks like its -6 even though its not.
Very basic explanations have been given but I think it helps to understand what’s happening in more detail. Basically floating point numbers have two main issues. One, they can only correctly store a limited number of digits (single can handle 7, double can handle 15). Two, they are inaccurate due to the way they are implemented.
Like has already been mentioned what you write as a 6 isn’t necessarily a 6 but the closest representation of a 6 that a floating point number is capable of representing. If you were just comparing two “6” values then this wouldn’t be a problem but you’re trying to compare a 6 to a number that has had math performed on it.
Performing math operations on a floating point number doesn’t just perform them on the parts you want but on every part of the floating point number meaning the inaccuracy doesn’t just carry over it becomes worse, and once you’ve performed enough of these the inaccuracies may make the number no longer close enough to represent what it should and thus an equal comparison (==) won’t be valid.
The way around this limitation is to use Mathf.Approximately which is designed to ignore the inaccuracy if it is within a certain margin of error allowing you to see if a number is close enough to what it should have been to qualify.
There are a number of good articles covering these problems (I’ll link a couple below with the first one being the easiest to understand and the second being much more detailed).
I don’t like Mathf.Approximately because the variance it allows for from the number you are checking against is very tight and doesn’t appear you can make it larger. So I usually just do something like this:
//Returns true if the compareValue float is within variance of the compareAgainst float
public static bool CompareFloat(float compareValue, float compareAgainst, float variance = 0.1f)
{
bool returnValue = false;
if ((compareValue <= (compareAgainst + variance)) && (compareValue >= (compareAgainst - variance)))
{
returnValue = true;
}
return returnValue;
}