/
GoodThought.cs
79 lines (66 loc) · 1.93 KB
/
GoodThought.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoodThought : MonoBehaviour
{
public Vector3 origin;
public float rotation = 0; // degrees around origin
public float radius = 6;
public float speed = 0.1f;
public float speedToCenter = 0.1f;
public bool attached = false;
public PlayerTrain train;
public GoodThought previous;
public GoodThought next;
public Transform attachmentFront;
public Transform attachmentBack;
// Start is called before the first frame update
public Transform targetAnchor;
public GameObject trainGFX;
void Start()
{
}
void FixedUpdate()
{
if (attached)
{
transform.up = targetAnchor.position - transform.position;
transform.position = targetAnchor.position - (transform.up * attachmentFront.localPosition.magnitude);
return;
}
radius = Mathf.Lerp(radius, 0, speedToCenter*Time.deltaTime);
Vector3 left = new Vector3(origin.x - radius, origin.y, origin.z);
Vector3 right = new Vector3(origin.x + radius, origin.y, origin.z);
rotation += Time.deltaTime * speed;
rotation = rotation % 2;
Vector3 xform;
if (rotation > 1)
{
xform = Vector3.Slerp(right, left, rotation - 1);
}
else
{
xform = Vector3.Slerp(left, right, rotation);
}
if (rotation > 1)
{
transform.position = new Vector3(xform.x, -xform.z, xform.y);
}
else
{
transform.position = new Vector3(xform.x, xform.z, xform.y);
}
}
public void AttachTo(Transform anchor)
{
targetAnchor = anchor;
attached = true;
trainGFX.SetActive(true);
}
public void Detatch()
{
targetAnchor = null;
attached = false;
trainGFX.SetActive(false);
}
}