MoveDirection.cs
24.7 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
namespace UnityEngine.EventSystems
{
/// <summary>
/// This is an 4 direction movement enum.
/// </summary>
/// <remarks>
/// MoveDirection provides a way of switching between moving states. You must assign these states to actions, such as moving the GameObject by an up vector when in the Up state.
/// Having states like these are easier to identify than always having to include a large amount of vectors and calculations.Instead, you define what you want the state to do in only one part, and switch to the appropriate state when it is needed.
/// </remarks>
/// <example>
/// <code>
/// //This is a full example of how a GameObject changes direction using MoveDirection states
/// //Assign this script to a visible GameObject (with a Rigidbody attached) to see it in action
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class Example : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_UpVector;
/// Vector3 m_RightVector;
/// const float speed = 5.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the y axis (for moving upwards)
/// m_UpVector = Vector3.up;
/// //This Vector is set to 1 in the x axis (for moving in the right direction)
/// m_RightVector = Vector3.right;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving in an upwards direction
/// case MoveDirection.Up:
/// //Change the velocity so that the Rigidbody travels upwards
/// m_Rigidbody.velocity = m_UpVector * speed;
/// break;
///
/// //This is for moving left
/// case MoveDirection.Left:
/// //This moves the Rigidbody to the left (minus right Vector)
/// m_Rigidbody.velocity = -m_RightVector * speed;
/// break;
///
/// //This is for moving right
/// case MoveDirection.Right:
/// //This moves the Rigidbody to the right
/// m_Rigidbody.velocity = m_RightVector * speed;
/// break;
///
/// //This is for moving down
/// case MoveDirection.Down:
/// //This moves the Rigidbody down
/// m_Rigidbody.velocity = -m_UpVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Left button to switch the Rigidbody direction to the left
/// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Left"))
/// {
/// //Switch to the left direction
/// m_MoveDirection = MoveDirection.Left;
/// }
///
/// //Press the Up button to switch the Rigidbody direction to upwards
/// if (GUI.Button(new Rect(100, 60, 150, 30), "Move Up"))
/// {
/// //Switch to Up Direction
/// m_MoveDirection = MoveDirection.Up;
/// }
///
/// //Press the Down button to switch the direction to down
/// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
/// {
/// //Switch to Down Direction
/// m_MoveDirection = MoveDirection.Down;
/// }
///
/// //Press the right button to switch to the right direction
/// if (GUI.Button(new Rect(100, 120, 150, 30), "Move Right"))
/// {
/// //Switch to Right Direction
/// m_MoveDirection = MoveDirection.Right;
/// }
/// }
/// }
/// </code>
/// </example>
public enum MoveDirection
{
/// <summary>
/// This is the Left state of MoveDirection. Assign functionality for moving to the left.
/// </summary>
/// <remarks>
/// Use the Left state for an easily identifiable way of moving a GameObject to the left (-1 , 0 , 0). This is a state without any predefined functionality. Before using this state, you should define what your GameObject will do in code.
/// </remarks>
/// <example>
/// <code>
/// //Assign this script to a visible GameObject (with a Rigidbody attached) to see this in action
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class Example : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_RightVector;
/// const float speed = 5.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the x axis (for moving in the right direction)
/// m_RightVector = Vector3.right;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving left
/// case MoveDirection.Left:
/// //This moves the Rigidbody to the left (minus right Vector)
/// m_Rigidbody.velocity = -m_RightVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Left button to switch the Rigidbody direction to the left
/// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Left"))
/// {
/// //Switch to the left direction
/// m_MoveDirection = MoveDirection.Left;
/// }
/// }
/// }
/// </code>
/// </example>
Left,
/// <summary>
/// This is the Up state of MoveDirection. Assign functionality for moving in an upward direction.
/// </summary>
/// <remarks>
/// Use the Up state for an easily identifiable way of moving a GameObject upwards (0 , 1 , 0). This is a state without any predefined functionality. Before using this state, you should define what your GameObject will do in code.
/// </remarks>
/// <example>
/// <code>
/// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Up" button in Game view to see it in action.
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class Example : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_UpVector;
/// const float speed = 10.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the y axis (for moving upwards)
/// m_UpVector = Vector3.up;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving in an upwards direction
/// case MoveDirection.Up:
/// //Change the velocity so that the Rigidbody travels upwards
/// m_Rigidbody.velocity = m_UpVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Up button to switch the Rigidbody direction to upwards
/// if (GUI.Button(new Rect(100, 60, 150, 30), "Move Up"))
/// {
/// //Switch to Up Direction
/// m_MoveDirection = MoveDirection.Up;
/// }
/// }
/// }
/// </code>
/// </example>
Up,
/// <summary>
/// This is the Right state of MoveDirection. Assign functionality for moving to the right.
/// </summary>
/// <remarks>
/// Use the Right state for an easily identifiable way of moving a GameObject to the right (1 , 0 , 0). This is a state without any predefined functionality. Before using this state, you should define what your GameObject will do in code.
/// </remarks>
/// <example>
/// <code>
/// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Right" button in Game view to see it in action.
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class MoveDirectionExample : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_RightVector;
/// const float speed = 5.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the x axis (for moving in the right direction)
/// m_RightVector = Vector3.right;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving right
/// case MoveDirection.Right:
/// //This moves the Rigidbody to the right
/// m_Rigidbody.velocity = m_RightVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Left button to switch the Rigidbody direction to the right
/// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Right"))
/// {
/// //Switch to the left direction
/// m_MoveDirection = MoveDirection.Right;
/// }
/// }
/// }
/// </code>
/// </example>
Right,
/// <summary>
/// The Down State of MoveDirection. Assign functionality for moving in a downward direction.
/// </summary>
/// <remarks>
/// Use the Down state for an easily identifiable way of moving a GameObject downwards (0 , -1 , 0). This is a state without any predefined functionality. Before using this state, you should define what your GameObject will do in code.
/// </remarks>
/// <example>
/// <code>
/// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Down" button in Game view to see it in action.
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class Example : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_UpVector;
/// const float speed = 10.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the y axis (for moving upwards)
/// m_UpVector = Vector3.up;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving down
/// case MoveDirection.Down:
/// //This moves the Rigidbody down
/// m_Rigidbody.velocity = -m_UpVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Down button to switch the direction to down
/// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
/// {
/// //Switch to Down Direction
/// m_MoveDirection = MoveDirection.Down;
/// }
/// }
/// }
/// </code>
/// </example>
Down,
/// <summary>
/// This is the None state. Assign functionality that stops movement.
/// </summary>
/// <remarks>
/// Use the None state for an easily identifiable way of stopping, resetting or initialising a GameObject's movement. This is a state without any predefined functionality. Before using this state, you should define what your GameObject will do in code.
/// </remarks>
/// <example>
/// <code>
/// //Attach this script to a GameObject with a Rigidbody attached. This script starts off on the ModeDirection.None state but changes depending on buttons you press.
///
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class Example : MonoBehaviour
/// {
/// Vector3 m_StartPosition, m_StartForce;
/// Rigidbody m_Rigidbody;
/// //Use Enum for easy switching between direction states
/// MoveDirection m_MoveDirection;
///
/// //Use these Vectors for moving Rigidbody components
/// Vector3 m_ResetVector;
/// Vector3 m_UpVector;
/// const float speed = 10.0f;
///
/// void Start()
/// {
/// //You get the Rigidbody component attached to the GameObject
/// m_Rigidbody = GetComponent<Rigidbody>();
/// //This starts with the Rigidbody not moving in any direction at all
/// m_MoveDirection = MoveDirection.None;
///
/// //These are the GameObject’s starting position and Rigidbody position
/// m_StartPosition = transform.position;
/// m_StartForce = m_Rigidbody.transform.position;
///
/// //This Vector is set to 1 in the y axis (for moving upwards)
/// m_UpVector = Vector3.up;
/// //This Vector is zeroed out for when the Rigidbody should not move
/// m_ResetVector = Vector3.zero;
/// }
///
/// void Update()
/// {
/// //This switches the direction depending on button presses
/// switch (m_MoveDirection)
/// {
/// //The starting state which resets the object
/// case MoveDirection.None:
/// //Reset to the starting position of the GameObject and Rigidbody
/// transform.position = m_StartPosition;
/// m_Rigidbody.transform.position = m_StartForce;
/// //This resets the velocity of the Rigidbody
/// m_Rigidbody.velocity = m_ResetVector;
/// break;
///
/// //This is for moving down
/// case MoveDirection.Down:
/// //This moves the Rigidbody down
/// m_Rigidbody.velocity = -m_UpVector * speed;
/// break;
/// }
/// }
///
/// void OnGUI()
/// {
/// //Press the reset Button to switch to no mode
/// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
/// {
/// //Switch to start/reset case
/// m_MoveDirection = MoveDirection.None;
/// }
///
/// //Press the Down button to switch the direction to down
/// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
/// {
/// //Switch to Down Direction
/// m_MoveDirection = MoveDirection.Down;
/// }
/// }
/// }
/// </code>
/// </example>
None
}
}