So when I was using oscillation for one of my levels I tried making it oscillate on the y axis but it just stays still doing nothing. I have double-checked my code with Rick’s and yet it doesn’t work. I’m not sure if this is on purpose or not and if it’s gotta do something with my code here you go:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Oscillator : MonoBehaviour
{
Vector3 startingPos;
[SerializeField] Vector3 movementVector;
float movementFactor;
[SerializeField] float period = 2f;
// Start is called before the first frame update
void Start()
{
startingPos = transform.position;
}
// Update is called once per frame
void Update()
{
if (period <= Mathf.Epsilon)
{
return;
}
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 = startingPos + offset;
}
}
Thanks!