I just need to share this, cuse im so proud of myself, i added rotation and scaling to the oscillator script, so you can apply everyting at once if you want to
public class Oscillator : MonoBehaviour
{
[Header("Movement Options")]
[SerializeField] Vector3 movementVector;
[SerializeField][Range(0f, 1f)] float movementFactor;
[SerializeField] float movementSpeed = 2f; //Sin Period, higher numbers is slower
[Header("Rotation Options")]
[SerializeField] Vector3 roatationVector;
[SerializeField][Range(0f, 1f)] float rotationFactor;
[SerializeField] float rotationSpeed = 2f; //Sin Period, higher numbers is slower
[Header("Scale Options")]
[SerializeField] Vector3 scalingVector;
[SerializeField][Range(0f, 1f)] float scalingFactor;
[SerializeField] float scalingSpeed = 2f; //Sin Period, higher numbers is slower
Vector3 startingPosition;
Quaternion startingRotation;
Vector3 startingScale;
const float pi = Mathf.PI;
const float tau = pi * 2;
private void Awake()
{
startingPosition = transform.position;
startingRotation = transform.rotation;
startingScale = transform.localScale;
}
private void Update()
{
ApplyMovement();
ApplyRotation();
ApplyScaling();
}
void ApplyMovement()
{
float cycles = Time.time / movementSpeed; // Continually growint over time
float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1
movementFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1
Vector3 offset = movementVector * movementFactor;
transform.position = startingPosition + offset;
}
void ApplyRotation()
{
float cycles = Time.time / rotationSpeed; // Continually growint over time
float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1
rotationFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1
Vector3 offset = roatationVector * rotationFactor;
transform.Rotate(offset);
}
void ApplyScaling()
{
float cycles = Time.time / scalingSpeed; // Continually growint over time
float rawSinWave = Mathf.Sin(cycles * tau); // going from -1 to 1
scalingFactor = (rawSinWave + 1) / 2; // recalculated to go from 0 to 1
Vector3 offset = scalingVector * scalingFactor;
transform.localScale = startingScale + offset;
}
}