using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OscillateRotation : MonoBehaviour
{
///
/// Curve that defines the pattern we rotate the transform over time.
///
[Tooltip("Curve that defines the pattern we rotate the transform over time.")]
public AnimationCurve rotateCurve = new AnimationCurve(new Keyframe[5]
{
new Keyframe(0, 0, 5, 5),
new Keyframe(0.25f, 1, 0, 0),
new Keyframe(0.5f, 0, -5, -5),
new Keyframe(0.75f, -1, 0, 0),
new Keyframe(1, 0, 5, 5)
});
///
/// How far the transform rotates on points on the curve equal to 1 or -1. (Technically not "max" but more intuitive than "normalized" or something.)
///
[Tooltip("How far the transform rotates on points on the curve equal to 1 or -1. (Technically not 'max' but more intuitive than 'normalized' or something.)")]
public float maxDegrees = 20f;
///
/// How long it takes for a full cycle, ie. playing through rotateCurve all the way.
///
[Tooltip("How long it takes for a full cycle, ie. playing through rotateCurve all the way.")]
public float cycleTimeSeconds = 2f;
///
/// Timer that keeps track of where we are in the rotateCurve at any given moment.
///
private float cycleTimer = 0f;
///
/// True to rotate on the X axis.
///
[Space(5)]
[Tooltip("True to rotate on the X axis.")]
public bool rotateOnX = false;
///
/// True to rotate on the Y axis.
///
[Tooltip("True to rotate on the Y axis.")]
public bool rotateOnY = false;
///
/// True to rotate on the Z axis.
///
[Tooltip("True to rotate on the Z axis.")]
public bool rotateOnZ = true;
public Vector3 startRotationEulers;
// Use this for initialization
void Start ()
{
startRotationEulers = transform.localRotation.eulerAngles;
}
// Update is called once per frame
void Update ()
{
cycleTimer += Time.deltaTime;
if (cycleTimer > cycleTimeSeconds) cycleTimer %= cycleTimeSeconds;
float rotateamount = rotateCurve.Evaluate(cycleTimer / cycleTimeSeconds) * maxDegrees;
float xfinal = rotateOnX ? startRotationEulers.x + rotateamount : transform.localEulerAngles.x;
float yfinal = rotateOnY ? startRotationEulers.y + rotateamount : transform.localEulerAngles.y;
float zfinal = rotateOnZ ? startRotationEulers.z + rotateamount : transform.localEulerAngles.z;
transform.localEulerAngles = new Vector3(xfinal, yfinal, zfinal);
}
}