Here it is below – I might be a mess, so if there is a better format for uploading feel free to ping.
I used the basic oscillator they taught and did a duplicate bit for the rotation. I commented out the controlled oscillation between two points for my code.
Function: private void OscRotation()
I also did some code to get obstacles to move in local space on one of my levels as an option in the movement oscillator.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour
{
[SerializeField] Vector3 movementVector = new Vector3 (10.0f, 10.0f, 10.0f);
[SerializeField] float period = 2.0f;
[Range(0, 1)] float movementFactor;
[SerializeField] Vector3 startingPostion; //must be stored for absolute movement
//[SerializeField] Vector3 endingPosition;
[SerializeField] Vector3 rotationVector = new Vector3(0.0f, 0.0f, 0.0f);
[SerializeField] float rotationPeriod;
[Range(0, 1)] float rotationFactor;
[SerializeField] Quaternion startingRotation;
[SerializeField] Boolean isLocal = false;
// Start is called before the first frame update
void Start()
{
startingPostion = transform.position;
startingRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
Movement();
OscRotation();
}
private void OscRotation()
{
//set movement factor
if (rotationPeriod <= Mathf.Epsilon)
{
return;
}
float cycles = Time.time / rotationPeriod; //grows continually from 0
const float tau = Mathf.PI * 2;
float rawSinWave = Mathf.Sin(cycles * tau);
rotationFactor = rawSinWave / 2 + 0.5f;
Vector3 offset = rotationFactor * rotationVector;
//transform.rotation = Quaternion.Euler(offset); //this was the controlled version osscilating between two points
transform.Rotate(offset); //this here cause the rotations to become out of control; however, if you run it you will notice there are points where the rotation slows down before winding back up -- I believe at the end of the sin waves
}
private void Movement()
{
//set movement factor
if (period <= Mathf.Epsilon)
{
return;
}
float cycles = Time.time / period; //grows continually from 0
const float tau = Mathf.PI * 2;
float rawSinWave = Mathf.Sin(cycles * tau);
movementFactor = rawSinWave / 2 + 0.5f;
Vector3 offset = movementVector * movementFactor;
if (!isLocal)
{
transform.position = startingPostion + offset;
}
else
{
transform.position = startingPostion + transform.TransformDirection(offset); // wanted to create the option to move the object according to the local axis
}
//transform.Translate(startingPostion + offset, Space.Self);
}
}