forked from vaibhav899050/PastClones-IGDC-GameDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingPlatform.cs
206 lines (167 loc) · 5.84 KB
/
MovingPlatform.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Different types of movements for the platform
public enum MovementType
{
line,
circular,
zigzag
};
// Movement orientation for line movement type
public enum LineMovementOrientation
{
horizontal,
vertical
}
// Movement orientation for circular movement type
public enum CircularMovementOrientation
{
clockwise,
counterclockwise
}
public class MovingPlatform : MonoBehaviour
{
// General vars (affect all types of movemens)
[SerializeField]
MovementType movementType;
public float speed = 2f;
Vector3 startPosition;
public Color gizmoColor = Color.yellow;
// Line movement vars
public LineMovementOrientation lineMovementOrientation;
public float lineDistance = 5f;
// Circle movement vars
public CircularMovementOrientation circularMovementOrientation;
public float circleRadius = 5f;
// Zigzag movement vars
public int zigzagLines = 4;
public float zigzagLineDistance = 2;
float zigzagStep;
bool zigzagMovingPositive = true;
// Start is called before the first frame update
void Start()
{
// Set start position
startPosition = this.transform.position;
zigzagStep = 0f;
}
// Update is called once per frame
void Update()
{
// Manage how the platform have to move according to movementType var
switch (movementType)
{
case MovementType.line:
moveInAStraightLine();
break;
case MovementType.circular:
MoveInCircles();
break;
case MovementType.zigzag:
MoveInZigzag();
break;
}
}
// Move the platform in a straight line in movementOrientation
public void moveInAStraightLine()
{
// Get current coordenates
float x = startPosition.x;
float y = startPosition.y;
float z = startPosition.z;
// Calculating next position according to the orientation selected
switch (lineMovementOrientation)
{
case LineMovementOrientation.horizontal:
x = startPosition.x + Mathf.Sin(Time.time * speed) * lineDistance;
break;
case LineMovementOrientation.vertical:
y = startPosition.y + Mathf.Sin(Time.time * speed) * lineDistance;
break;
}
// Moving platform
this.transform.position = new Vector3(x, y, z);
}
public void MoveInCircles()
{
// Calculating direction (CW or CCW)
int direction = (circularMovementOrientation == CircularMovementOrientation.counterclockwise) ? 1 : -1;
// Calculating coordenates
float x = startPosition.x + Mathf.Cos(Time.time * speed * direction) * circleRadius;
x -= circleRadius;
float y = startPosition.y + Mathf.Sin(Time.time * speed * direction) * circleRadius;
float z = transform.position.z;
// Moving Platform
this.transform.position = new Vector3(x, y, z);
}
public void MoveInZigzag()
{
// Changing direction when the platform reach the limits
if (transform.position.x >= startPosition.x + zigzagLineDistance * zigzagLines)
{
zigzagMovingPositive = false;
}
else if (transform.position.x <= startPosition.x)
{
zigzagMovingPositive = true;
}
// Calculating coordenates
float factor = (Mathf.Acos(Mathf.Cos(zigzagStep * (float)Math.PI)) / (float)Math.PI);
float x = startPosition.x + zigzagStep * zigzagLineDistance;
float y = startPosition.y + factor * zigzagLineDistance;
if (zigzagMovingPositive)
{
zigzagStep += speed / 50;
}
else
{
zigzagStep -= speed / 50;
}
// keep platform within the limits
zigzagStep = Mathf.Clamp(zigzagStep, 0, zigzagLines);
// Moving platform
this.transform.position = new Vector3(x, y);
}
// Funtion to see the platform path (only for debugging)
private void OnDrawGizmosSelected()
{
Gizmos.color = gizmoColor;
Vector3 src = Vector3.zero;
Vector3 dest = Vector3.zero;
switch (movementType)
{
case MovementType.line:
src = new Vector3(startPosition.x - lineDistance, startPosition.y);
dest = new Vector3(startPosition.x + lineDistance, startPosition.y);
Gizmos.DrawLine(src, dest);
src = new Vector3(startPosition.x, startPosition.y - lineDistance);
dest = new Vector3(startPosition.x, startPosition.y + lineDistance);
Gizmos.DrawLine(src, dest);
break;
case MovementType.circular:
// Cicular movement
src = new Vector3(startPosition.x - circleRadius, startPosition.y);
Gizmos.DrawWireSphere(src, circleRadius);
break;
case MovementType.zigzag:
float x = startPosition.x;
float y = startPosition.y;
for (int i = 0; i < zigzagLines; i++)
{
// Current position
src = new Vector3(x, y);
// Calculating next position
x += zigzagLineDistance;
// If "i" is even draw line going up, else, draw line going down
// the zigzag movement always start going up
y = (i % 2 == 0) ? startPosition.y + zigzagLineDistance : startPosition.y;
dest = new Vector3(x, y);
// Drawing line
Gizmos.DrawLine(src, dest);
}
break;
}
}
}