I had to take a moment to test and double check but I did find a weird problem with the lecturers code.
If the starting Z-position of the oscillating object is 0 the function works fine.
However if the starting Z is NOT zero then the target position goes off.
Starting Z value at Zero line drawn from Start to “movementVector”
Starting Z value et -10 and line drawn from Start to “movementVector”
Here the code used in these examples
public class Oscillator : MonoBehaviour
{
Vector3 startingPosition;
[SerializeField] Vector3 movementVector;
float movementFactor;
[SerializeField] float period = 2f;
private void Start() {
startingPosition = transform.position;
}
void Update()
{
float cycles = Time.time / period; // continually growing over time
const float tau = Mathf.PI * 2; // constant value of 6.283
float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1
movementFactor = (rawSinWave + 1f) / 2f; // recalculated to go from 0 to 1 so its cleaner
Vector3 offset = movementVector * movementFactor;
transform.position = startingPosition + offset;
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(startingPosition, movementVector);
}
}
Does anyone know what could be causing this? For some reason X and Y values don’t effect the behavior. This Sphere I used for testing is not parented to anything it’s an empty primitive for testing.
For some reason this only happens with the startingPosition Z value and nothing else (so far)