forked from renaissanceCoder/Simple-Scripting-Series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slerper.cs
42 lines (37 loc) · 1.32 KB
/
Slerper.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
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Slerper : MonoBehaviour {
public Transform startPos;
public Transform endPos;
public float journeyTime = 1.0f;
public float speed;
public bool repeatable;
float startTime;
Vector3 centerPoint;
Vector3 startRelCenter;
Vector3 endRelCenter;
// Update is called once per frame
void Update () {
GetCenter(Vector3.up);
if(!repeatable) {
float fracComplete = (Time.time - startTime) / journeyTime * speed;
transform.position = Vector3.Slerp(startRelCenter, endRelCenter, fracComplete * speed);
transform.position += centerPoint;
}
if (repeatable) {
float fracComplete = Mathf.PingPong(Time.time - startTime, journeyTime / speed);
transform.position = Vector3.Slerp(startRelCenter, endRelCenter, fracComplete * speed);
transform.position += centerPoint;
if (fracComplete >= 1) {
startTime = Time.time;
}
}
}
public void GetCenter(Vector3 direction) {
centerPoint = (startPos.position + endPos.position) * .5f;
centerPoint -= direction;
startRelCenter = startPos.position - centerPoint;
endRelCenter = endPos.position - centerPoint;
}
}