I solved the NaN issue slightly differently

The difference is that instead of exiting the method, it will set the movement factor to zero if the period is too small.

    // Prevent dividing by zero
    if (period <= Mathf.Epsilon) movementFactor = 0f;
    else movementFactor = Mathf.Sin(Time.time * 2 * Mathf.PI / period);

    transform.position = initialPosition + movementFactor * movementVector;

Privacy & Terms