-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOscillator.cs
38 lines (29 loc) · 937 Bytes
/
Oscillator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour {
[SerializeField] Vector3 movementVector;//Vector to move along
[SerializeField] float period;//Period of oscilliation
Vector3 startingPos;//Store starting position
float movementFactor;//0 for not moved, 1 for fully moved
// Use this for initialization
void Start ()
{
startingPos = transform.position;
}
// Update is called once per frame
void Update ()
{
if (period <= Mathf.Epsilon)
{
return;
}
float cycles = Time.time / period;
const float tau = 2 * Mathf.PI;
float rawSinWave = Mathf.Sin(cycles * tau);
movementFactor = rawSinWave / 2f + 0.5f;
Vector3 offset = movementFactor * movementVector;
transform.position = startingPos + offset;
}
}