Two ways to prevent from NaN error

Here is what I found that works:
1- add [Range(1, 100)] to the float period
2- add an if statement before cycles:

 if (period > 0)
        {
            float cycles = Time.time / period; //grows contiually from 0
            const float tau = Mathf.PI * 2f; //creating a tau, about 6.8
            float rawSinWave = Mathf.Sin(cycles * tau); //moves -1 to 1
            movementFactor = rawSinWave / 2f + 0.5f;

            Vector3 offset = movementVector * movementFactor; //calculate offset by multiplying the difference of space selected and speed selected
            transform.position = startingPos + offset; //change position
        }
1 Like

Awesome tips! These two ways will help a lot of people.

Privacy & Terms