44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace RR.Common.Behaviour
|
|
{
|
|
public class Oscillator : MonoBehaviour
|
|
{
|
|
[Header("Motion")]
|
|
[SerializeField] private Vector3 localAxis = Vector3.forward;
|
|
[SerializeField, Min(0f)] private float amplitude = 20f;
|
|
[SerializeField, Min(0f)] public float frequency = 1f;
|
|
[SerializeField] private float phaseOffsetDeg = 0f;
|
|
[SerializeField] private bool randomizePhaseOnEnable = false;
|
|
[SerializeField] private bool useUnscaledTime = false;
|
|
[SerializeField] private bool resetBaseOnEnable = true;
|
|
|
|
private Quaternion _baseLocalRotation;
|
|
private float _phaseRad;
|
|
|
|
void Awake()
|
|
{
|
|
_baseLocalRotation = transform.localRotation;
|
|
_phaseRad = phaseOffsetDeg * Mathf.Deg2Rad;
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
if (resetBaseOnEnable)
|
|
_baseLocalRotation = transform.localRotation;
|
|
|
|
if (randomizePhaseOnEnable)
|
|
_phaseRad = Random.value * Mathf.PI * 2f;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Vector3 axis = localAxis.sqrMagnitude > 1e-6f ? localAxis.normalized : Vector3.forward;
|
|
float t = useUnscaledTime ? Time.unscaledTime : Time.time;
|
|
|
|
float angle = amplitude * Mathf.Sin(2f * Mathf.PI * frequency * t + _phaseRad);
|
|
transform.localRotation = _baseLocalRotation * Quaternion.AngleAxis(angle, axis);
|
|
}
|
|
}
|
|
}
|