-
Notifications
You must be signed in to change notification settings - Fork 1
/
Conveyor.cs
137 lines (115 loc) · 4.48 KB
/
Conveyor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Linq;
using UnityEngine;
using UnityEngine.Splines;
public class Conveyor : MonoBehaviour
{
public GameObject trackItem,start,end;
[Range(.1f,1f)]
public float spacing =.5f;
[Range(.5f,10f)]
public float speed = 1f;
Vector3 startPos,endPos;
float setSpacing ,setSpeed;
BoxCollider boxCollider ;
SplineContainer path;
Rigidbody rb ;
SplineAnimate[] tracks = {};
void Start()
{
Vector3[] knotPos = {
new Vector3 { x = 0, y = 1, z = -.5f },
new Vector3 { x = 0, y = 1, z = .5f },
new Vector3 { x = 0, y = .5f, z = 1 },
new Vector3 { x = 0, y = 0, z = .5f },
new Vector3 { x = 0, y = 0, z = -.5f },
new Vector3 { x = 0, y = .5f, z = -1},
};
Vector3[] knotTan = {
new Vector3 { x = 0, y = 0, z = .1f},
new Vector3 { x = 0, y = 0, z = .1f},
new Vector3 { x = 0, y = 0, z = .5f},
new Vector3 { x = 0, y = 0, z = .1f},
new Vector3 { x = 0, y = 0, z = .1f},
new Vector3 { x = 0, y = 0, z = .5f},
};
Quaternion[] knotRot = {
Quaternion.Euler(0,0,0),
Quaternion.Euler(0,0,0),
Quaternion.Euler(90,0,0),
Quaternion.Euler(0,180,180),
Quaternion.Euler(0,180,180),
Quaternion.Euler(270,0,0),
};
boxCollider = gameObject.AddComponent(typeof(BoxCollider)) as BoxCollider;
boxCollider.center = new Vector3(0, .5f,0);
rb = gameObject.AddComponent(typeof(Rigidbody)) as Rigidbody;
rb.isKinematic = true;
path = gameObject.AddComponent(typeof(SplineContainer)) as SplineContainer;
for (int i = 0; i < knotPos.Length; i++)
{
var knot = new BezierKnot(knotPos[i],knotTan[i],knotTan[i],knotRot[i]);
path.Spline.Add(knot,TangentMode.Mirrored);
}
path.Spline.Closed = true;
Resize();
}
void Resize(){
for (int i = 0; i < tracks.Length; i++)
{
Destroy(tracks[i].gameObject);
}
startPos = start.transform.position;
endPos = end.transform.position;
setSpacing = spacing;
setSpeed = speed;
var pathVector = endPos - startPos;
transform.LookAt(endPos);
transform.position = start.transform.position + (pathVector / 2);
var length = Mathf.RoundToInt(pathVector.magnitude);
var knots = path.Spline.Knots.ToArray();
knots[0].Position.z = ((length /2f ) -.5f) *-1f ;
knots[1].Position.z = (length /2f ) -.5f ;
knots[2].Position.z = length /2f ;
knots[3].Position.z = (length /2f ) -.5f ;
knots[4].Position.z = ((length /2f ) -.5f) *-1f ;
knots[5].Position.z = length /2f * -1f;
for (int i = 0; i < knots.Length; i++)
{
path.Spline.SetKnot(i,knots[i]);
}
boxCollider.size = new Vector3(1, 1, length);
var trackCount = Mathf.RoundToInt(path.Spline.GetLength()/setSpacing);
tracks = new SplineAnimate[trackCount];
float offsetIncreament = 1f / trackCount;
for (int i = 0; i < trackCount; i++)
{
var newTrack = Instantiate(trackItem, new Vector3(0, 0, 0), Quaternion.identity);
newTrack.transform.parent = transform;
tracks[i] = newTrack.AddComponent(typeof(SplineAnimate)) as SplineAnimate;
tracks[i].Container = path;
tracks[i].StartOffset = offsetIncreament * i;
tracks[i].AnimationMethod = SplineAnimate.Method.Speed;
tracks[i].MaxSpeed = speed;
}
}
void Update()
{
if (startPos != start.transform.position || endPos != end.transform.position || setSpacing != spacing || setSpeed != speed){
Resize();
}
}
void OnCollisionEnter(Collision collision)
{
var rb = collision.gameObject.GetComponent<Rigidbody>();
rb.useGravity = false;
rb.isKinematic = true;
collision.gameObject.transform.LookAt(collision.gameObject.transform.position + transform.forward);
rb.isKinematic = false;
rb.velocity = transform.forward * setSpeed;
}
void OnCollisionExit(Collision collision)
{
var rb = collision.gameObject.GetComponent<Rigidbody>();
rb.useGravity = true;
}
}