Instead of creating vars for ‘cycles’ and ‘tau’ (which went a little over my head, tbh) I just added in the following statement inside Update():
movementFactor = Mathf.Sin(Time.time)
This worked a treat for me and allowed the platform to oscillate nicely back and forth without any of the headache trying to understand tau, radians, etc.
Can anyone please explain to me why my solution is sub-optimal when compared with Rick’s? (I’m sure it probably is but - since it seems to achieve the same results - I’m not sure why…)
it works because that’s exactly how the Sin function works. Time.time returns the amount of time since the game started, and so it is a value that increases and increases, whereas the Sin functional will constantly and smoothly oscillate between -1 and 1 as the input value you send it increases
I spent some time to understand what’s going on, maybe it will be useful)
Dividing current time to period variable is needed to control speed of movement. In this case it slows down it in 2 times.
Multiplying to tau is needed to get radians.
In your case Time.time gives a float value. And between 0 to 6.2… function draws full cycle or Circumference, gives value from 0 to 1 from 1 to 0 then it repeat process. But the function thinks that you are feeding it with radians)
The main perk to doing it Rick’s way is that you get the ability to change the period value in the editor to make it complete a full cycle faster / slower. The “offset” is basically how far you want the obstacle to travel, and then period is how quickly you want it to travel.
There’s other ways to acheive this using your method but a lot of the time in game design you’re sort of building and engine inside an engine so that you can design the other features on top without creating bugs or having to do weird hacks to get a desired outcome
Ummm you can control the cycle here too, due to the fact that Time.time returns (real) time since started, so it is time frame independent and can be combined with any constant variable
[SerializeField] float cycleFactor = 1f; // can be changed in the inspector
movementFactor = Mathf.Sin(Time.time * cycleFactor)