Leave zero out of this!

In this video, we were tasked to find two ways to prevent a division by 0 for the period value. In order to do so, I employed the following:

Range(1, 10) //sets a range which excludes 0 and below
[SerializeField]
 float period = 2f; 

In that procedure, I prevent the value from even being an option, to begin with by setting the range to exclude numbers below 1.

Alternatively, I also created an if statement to prevent any arithmetic from executing if a 0 or less value is detected for the period.

if(period > 0)
        {
        float cycles = Time.time / period; //grows continally from 0
        //Time.time is framerate independent
        const float tau = Mathf.PI * 2;// about 6.29 this is for the radians
        float rawSinWave = Mathf.Sin(cycles * tau);
        print(rawSinWave);
        movementFactor = rawSinWave / 2f + 0.5f; //we add half because we want to go between 0 and 1
        Vector3 offset = movementVector * movementFactor;
        transform.position = startingPos + offset;
        }

I’m sure there are more optimal ways to address this, however, at this point in time, my video is still paused at the challenged screen :slight_smile:

Thanks for your time!

Privacy & Terms