NAN challenge

Here was my solution to avoid the NAN error.

	void Update () {
        // setMovementFactor

        float cycles = 0;
        if (period != 0)        
        {
            cycles = Time.time / period;  // Time.time grows continually from 0
        }
        const float tau = Mathf.PI * 2;  // about 6.28
        float rawSinWave = Mathf.Sin(cycles * tau); // goes from -1 to 1
        print(rawSinWave);
        movementFactor = rawSinWave / 2f + .5f;
        transform.position = startingPos + (movementVector * movementFactor);
	}
1 Like

Another messier way I thought of was just to add a very small number to the equation

void Update () {
    // protect against period = 0
    float cycles = Time.time / (period + 0.000001f);
    const float tau = Mathf.PI * 2;
    float rawSinWave = Mathf.Sin(cycles * tau);

    float movementFactor = rawSinWave / 2f + 0.5f;

    Vector3 offset = movementVector * movementFactor;
    transform.position = startingPos + offset;
}

Not posting my code since it is the way I did mine.

Privacy & Terms