You can do at least two different approaches: The first one rounds the float to two decimal places to avoid floating point imprecision.
private int _iteration;
private float _timer = 0f;
void FixedUpdate()
{
if ( _timer < 0.2f )
{
_iteration++;
_timer += Time.fixedDeltaTime;
_timer = ( float )System.Math.Round( _timer, 2 );
Debug.Log( "Iteration: " + _iteration + " | Timer: " + _timer );
}
}
-
The second one uses Mathf.Approximately to compare two floats using Epsilon as a tolerance.
private int _iteration;
private float _timer = 0f;
void FixedUpdate()
{
if ( !Mathf.Approximately( _timer, .2f ) )
{
_iteration++;
_timer += Time.fixedDeltaTime;
Debug.Log( "Iteration: " + _iteration + " | Timer: " + _timer );
}
}
↧