using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Makes an object slide on its X axis according to the AnimationCurve specified.
/// Used in the ZED Dark Room example scene to make lights sweep back and forth.
///
public class PitchOscillator : MonoBehaviour
{
///
/// The path it takes to oscillate. Makes it simple to set a pattern in the Inspector.
///
[Tooltip("The path it takes to oscillate. Makes it simple to set a pattern in the Inspector. ")]
public AnimationCurve animationCurve;
///
/// How long a full oscillation lasts (from start to finish of animationCurve).
///
[Tooltip("How long a full oscillation lasts (from start to finish of animationCurve). ")]
public float secondsPerOscillation = .95f;
///
/// Scales the values in animationCurve, since it's difficult to specify values outside -1 and 1 in the Inspector.
///
[Tooltip("Scales the values in animationCurve, since it's difficult to specify values outside -1 and 1 in the Inspector. ")]
public float distanceScale = 2;
///
/// How long through the animation it has played.
/// Incremented by Time.deltaTime / distanceScale each Update().
///
private float timer = 0f;
///
/// Cache for the starting position, so oscillations can be done relative to it after it moves.
///
private Vector3 startposition; //In local space
// Use this for initialization
void Start ()
{
startposition = transform.localPosition;
}
// Update is called once per frame
void Update ()
{
//Update the timer and restart the animationCurve if finished.
timer += Time.deltaTime;
if(timer >= secondsPerOscillation)
{
timer = timer % secondsPerOscillation;
}
//Move the light according to the curve.
float newxpos = animationCurve.Evaluate(timer / secondsPerOscillation) * distanceScale;
transform.localPosition = startposition + transform.localRotation * Vector3.right * newxpos;
}
}