I found this lesson a little confusing.
After doing a bit more research I found a much easier solution that still uses MathF.Sin that gives a nice smooth oscillating movement. It makes a lot more sense to me.
It uses the Lerp function which returns a number between two numbers…given a percentage.
Unity Vector3.Lerp Function
using UnityEngine;
[DisallowMultipleComponent]
public class MovingObject : MonoBehaviour
{
[Tooltip("The direction and amount the object must be moved, positive and negative from starting position.")]
[SerializeField] Vector3 MovementRange;
[Range(0.1f, 10f)]
[Tooltip("How fast the object is moving")]
[SerializeField] float MovementSpeed = 2f;
private Vector3 startingPosition;
// Use this for initialization
void Start()
{
startingPosition = transform.position;
}
// Update is called once per frame
void Update()
{
var movementFactor = Mathf.Sin(Time.time * MovementSpeed);
movementFactor = movementFactor / 2f + 0.5f; //We need a value between 0 and 1
transform.position = Vector3.Lerp(startingPosition - MovementRange, startingPosition + MovementRange, movementFactor);
}
}