20 is not equal to 20 while using Vector3.MoveTowards


I use Vector3.MoveTowards to return a camera to it’s original x rotation.

while(xRotation.localEulerAngles.x != patrolXRotation){
			yield return new WaitForEndOfFrame();
			xRotation.localEulerAngles = Vector3.MoveTowards(xRotation.localEulerAngles, new Vector3(patrolXRotation, xRotation.localEulerAngles.y, xRotation.localEulerAngles.z), Time.deltaTime * 10);

Well… the while loop is never ending.

Debug.Log(xRotation.localEulerAngles.x + ", " + patrolXRotation + ", " + (xRotation.localEulerAngles.x == patrolXRotation));

returns this:

20, 20, False

With target values like 20.0001 it sometimes work but not always.
Any ideas?

I had a similar problem and I took a lazy route of just measuring the time and changing it after the time expired.

I thought about a way to fix it properly and the way I came up with is setting a buffer float for the rotation and checking if the rotation.x < startPos.x + buffer && rotation.x > startPos - buffer.x

Bit of a pain to write out in code i expect and since I am still very new I expect there is a much better way. But im fairly sure this would work

there is a function for this type of calculations: Unity - Scripting API: Mathf.Approximately