Alright, how come no-one else suggested this?

My first thought - if period is greater than 0 then do the movement, otherwise - don’t.

void Update ()
    {
        // set movement factor

        if (period > 0f)
        {

            float cycles = Time.time / period; //grows continually from 0

            const float tau = Mathf.PI * 2; //about 6.28
            float rawSineWave = Mathf.Sin(cycles * tau); //cycles 0-1

            print(rawSineWave);

            movementFactor = rawSineWave / 2f + 0.5f;
            Vector3 offset = movementVector * movementFactor;
            transform.position = startingPos + offset;
        }
	}

So I got no warning in Visual Studio, but that could be due to lack of extensions that warn about things, as other posters have highlighted. Is this as bad/worse than the == technique? Which, coincidentally, didn’t actually occur to me at all. I don’t know if that’s good or bad!

1 Like

Nice! I actually came up with the same idea during the NAN challenge. My code ended up identical to yours and it seems to work in Unity with no errors.

Stack overflow is your friend. Seriously, I use this site every day in my professional development.

Check that out and hope it clarifies some questions you may have.

Cheers

Privacy & Terms