My solution, it seems to work, but is it okay?

here is my protection against NaN. I am wondering since it gives me no problems (or yellow underline), but is totally different than Ben’s, if it is a potential minefield.

 if (period != 0f)
        {
            float cycles = Time.time / period;
            const float tau = Mathf.PI * 2;
            float rawSineWave = Mathf.Sin(cycles * tau);
            float movementFactor = rawSineWave / 2f + 0.5f;
            Vector3 offset = movementVector * movementFactor;
            transform.position = startingPos + offset;
        }

That is basically what I did too, and I have the same question. I have quite a bit of code at this point that is different than what Ben has done and even though my level seems to work fine, I keep wondering if I am really hurting myself by not just using his code.

I also did it this way. I believe Period=0 needs to be allowed from a game design point of view. Auto-adjusting it to anything other than zero would impart movement on an object that you might need at some point to be stationary.

I am thinking about naming my scripts with a _js at the end so that I can swap back and forth between the teacher’s code. That way I can wander off and do my own thing, but still be able to work off of his code if I need to.

Same solution, I also wonder if this would cause any unforeseen problems.

1 Like

That was my first idea, but I thought of a better one.

Using an if statement still allows users to set the period to 0. The object will stop moving, however in order to prevent this a better way would be to assign it a range, with a minimum value of 0.01. This would make the object move very fast, but we would never get an error.

As you can see below, I set the maximum to 10, because moving over 10 periods is very slow, especially if you’re moving the object a long distance.

[Range(0.01f, 10f)] [SerializeField] float period = 2f;

Check out my post for a screenshot proving the range method works.

Privacy & Terms