SmoothDamp Problem/If Statement Inequality Problem [Solved]

Ok, so, what is happening is, i SmoothDamp a number, and then it reaches its goal. When i check that its reached its goal with an if statement, it evaluates to false sometimes, even after its reached its goal.

Script:

var yVel = 0.0;
	
var smoothPos = Mathf.SmoothDamp(transform.position.y, 
		initialPos - (height + .1), yVel, .01);
	transform.position.y = smoothPos;
	
	if (transform.position.y == (initialPos - (height + .1)))
	{
		//renderer.enabled = false;
		isActive = false;
		deactivating = false;
	}

The position ends up being the desired value (In this case: initialPos - (height + .1) )

Most often the if statement evaluates to false once the smooth is done.

And to verify i even tried debugging by printing the transform.position.y and the (initialPos - (height + .1)) and they are indeed equal once the smooth is finished. Yet the if statement evaluates to false. Lol.

Any help would be greatly appreciated. Thanks!

You’re dealing with floating point numbers. As such, you have a good chance that the numbers will be a tiny tiny bit off, and thus not perfectly equal. You need to check if they’re within something like .0001 of each other.

Agreed. Lol. Totally slipped my mind, rule of thumb, never compare floats! Lol, forgot about that one ;). Anyways, for anyone else looking, solved not by rounding or truncating as everyone always seems to suggest (its hard to control whether to round up/down and then comparing them might be bad if they both just round down but arent originally close enough to the accuracy you want etc), i used Mathf.Approximately(f1, f2)

Thanks!