Protecting against NaN - is this wrong?

Mmm… I just used an if statement, and encapsulated the rest of the oscillating code, my logic was that the flip side would nullify the effect altogether.

In other words IF period is greater than zero then DO ALL THIS STUFF and if it is not greater than zero (else) do nothing.

So in other words the oscillation will only react if there exists a number above 0 applied to the period float value.

visual studio did not complain at all… does this fall within the same or similar example that Ben gave about comparing floating point values, or is this approach acceptable?

(I was so proud for figuring it out all by myself too!)

if (period > 0)

    {

        float cycles = Time.time / period;

        const float tau = Mathf.PI * 2f;

        float rawSinWave = Mathf.Sin(cycles * tau);

        movementFactor = rawSinWave / 2f + .5f;

        Vector3 offset = movementVector * movementFactor;

        transform.position = startingPos + offset;

    }

Hi Goblin,

Your solution looks fine. Well done. In many cases, there is more than one way to make something work in C# and/or Unity.


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms