MoveDirection.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. namespace UnityEngine.EventSystems
  2. {
  3. /// <summary>
  4. /// This is an 4 direction movement enum.
  5. /// </summary>
  6. /// <remarks>
  7. /// 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.
  8. /// 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.
  9. /// </remarks>
  10. /// <example>
  11. /// <code>
  12. /// //This is a full example of how a GameObject changes direction using MoveDirection states
  13. /// //Assign this script to a visible GameObject (with a Rigidbody attached) to see it in action
  14. ///
  15. /// using UnityEngine;
  16. /// using UnityEngine.EventSystems;
  17. ///
  18. /// public class Example : MonoBehaviour
  19. /// {
  20. /// Vector3 m_StartPosition, m_StartForce;
  21. /// Rigidbody m_Rigidbody;
  22. /// //Use Enum for easy switching between direction states
  23. /// MoveDirection m_MoveDirection;
  24. ///
  25. /// //Use these Vectors for moving Rigidbody components
  26. /// Vector3 m_ResetVector;
  27. /// Vector3 m_UpVector;
  28. /// Vector3 m_RightVector;
  29. /// const float speed = 5.0f;
  30. ///
  31. /// void Start()
  32. /// {
  33. /// //You get the Rigidbody component attached to the GameObject
  34. /// m_Rigidbody = GetComponent<Rigidbody>();
  35. /// //This starts with the Rigidbody not moving in any direction at all
  36. /// m_MoveDirection = MoveDirection.None;
  37. ///
  38. /// //These are the GameObject’s starting position and Rigidbody position
  39. /// m_StartPosition = transform.position;
  40. /// m_StartForce = m_Rigidbody.transform.position;
  41. ///
  42. /// //This Vector is set to 1 in the y axis (for moving upwards)
  43. /// m_UpVector = Vector3.up;
  44. /// //This Vector is set to 1 in the x axis (for moving in the right direction)
  45. /// m_RightVector = Vector3.right;
  46. /// //This Vector is zeroed out for when the Rigidbody should not move
  47. /// m_ResetVector = Vector3.zero;
  48. /// }
  49. ///
  50. /// void Update()
  51. /// {
  52. /// //This switches the direction depending on button presses
  53. /// switch (m_MoveDirection)
  54. /// {
  55. /// //The starting state which resets the object
  56. /// case MoveDirection.None:
  57. /// //Reset to the starting position of the GameObject and Rigidbody
  58. /// transform.position = m_StartPosition;
  59. /// m_Rigidbody.transform.position = m_StartForce;
  60. /// //This resets the velocity of the Rigidbody
  61. /// m_Rigidbody.velocity = m_ResetVector;
  62. /// break;
  63. ///
  64. /// //This is for moving in an upwards direction
  65. /// case MoveDirection.Up:
  66. /// //Change the velocity so that the Rigidbody travels upwards
  67. /// m_Rigidbody.velocity = m_UpVector * speed;
  68. /// break;
  69. ///
  70. /// //This is for moving left
  71. /// case MoveDirection.Left:
  72. /// //This moves the Rigidbody to the left (minus right Vector)
  73. /// m_Rigidbody.velocity = -m_RightVector * speed;
  74. /// break;
  75. ///
  76. /// //This is for moving right
  77. /// case MoveDirection.Right:
  78. /// //This moves the Rigidbody to the right
  79. /// m_Rigidbody.velocity = m_RightVector * speed;
  80. /// break;
  81. ///
  82. /// //This is for moving down
  83. /// case MoveDirection.Down:
  84. /// //This moves the Rigidbody down
  85. /// m_Rigidbody.velocity = -m_UpVector * speed;
  86. /// break;
  87. /// }
  88. /// }
  89. ///
  90. /// void OnGUI()
  91. /// {
  92. /// //Press the reset Button to switch to no mode
  93. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  94. /// {
  95. /// //Switch to start/reset case
  96. /// m_MoveDirection = MoveDirection.None;
  97. /// }
  98. ///
  99. /// //Press the Left button to switch the Rigidbody direction to the left
  100. /// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Left"))
  101. /// {
  102. /// //Switch to the left direction
  103. /// m_MoveDirection = MoveDirection.Left;
  104. /// }
  105. ///
  106. /// //Press the Up button to switch the Rigidbody direction to upwards
  107. /// if (GUI.Button(new Rect(100, 60, 150, 30), "Move Up"))
  108. /// {
  109. /// //Switch to Up Direction
  110. /// m_MoveDirection = MoveDirection.Up;
  111. /// }
  112. ///
  113. /// //Press the Down button to switch the direction to down
  114. /// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
  115. /// {
  116. /// //Switch to Down Direction
  117. /// m_MoveDirection = MoveDirection.Down;
  118. /// }
  119. ///
  120. /// //Press the right button to switch to the right direction
  121. /// if (GUI.Button(new Rect(100, 120, 150, 30), "Move Right"))
  122. /// {
  123. /// //Switch to Right Direction
  124. /// m_MoveDirection = MoveDirection.Right;
  125. /// }
  126. /// }
  127. /// }
  128. /// </code>
  129. /// </example>
  130. public enum MoveDirection
  131. {
  132. /// <summary>
  133. /// This is the Left state of MoveDirection. Assign functionality for moving to the left.
  134. /// </summary>
  135. /// <remarks>
  136. /// 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.
  137. /// </remarks>
  138. /// <example>
  139. /// <code>
  140. /// //Assign this script to a visible GameObject (with a Rigidbody attached) to see this in action
  141. ///
  142. /// using UnityEngine;
  143. /// using UnityEngine.EventSystems;
  144. ///
  145. /// public class Example : MonoBehaviour
  146. /// {
  147. /// Vector3 m_StartPosition, m_StartForce;
  148. /// Rigidbody m_Rigidbody;
  149. /// //Use Enum for easy switching between direction states
  150. /// MoveDirection m_MoveDirection;
  151. ///
  152. /// //Use these Vectors for moving Rigidbody components
  153. /// Vector3 m_ResetVector;
  154. /// Vector3 m_RightVector;
  155. /// const float speed = 5.0f;
  156. ///
  157. /// void Start()
  158. /// {
  159. /// //You get the Rigidbody component attached to the GameObject
  160. /// m_Rigidbody = GetComponent<Rigidbody>();
  161. /// //This starts with the Rigidbody not moving in any direction at all
  162. /// m_MoveDirection = MoveDirection.None;
  163. ///
  164. /// //These are the GameObject’s starting position and Rigidbody position
  165. /// m_StartPosition = transform.position;
  166. /// m_StartForce = m_Rigidbody.transform.position;
  167. ///
  168. /// //This Vector is set to 1 in the x axis (for moving in the right direction)
  169. /// m_RightVector = Vector3.right;
  170. /// //This Vector is zeroed out for when the Rigidbody should not move
  171. /// m_ResetVector = Vector3.zero;
  172. /// }
  173. ///
  174. /// void Update()
  175. /// {
  176. /// //This switches the direction depending on button presses
  177. /// switch (m_MoveDirection)
  178. /// {
  179. /// //The starting state which resets the object
  180. /// case MoveDirection.None:
  181. /// //Reset to the starting position of the GameObject and Rigidbody
  182. /// transform.position = m_StartPosition;
  183. /// m_Rigidbody.transform.position = m_StartForce;
  184. /// //This resets the velocity of the Rigidbody
  185. /// m_Rigidbody.velocity = m_ResetVector;
  186. /// break;
  187. ///
  188. /// //This is for moving left
  189. /// case MoveDirection.Left:
  190. /// //This moves the Rigidbody to the left (minus right Vector)
  191. /// m_Rigidbody.velocity = -m_RightVector * speed;
  192. /// break;
  193. /// }
  194. /// }
  195. ///
  196. /// void OnGUI()
  197. /// {
  198. /// //Press the reset Button to switch to no mode
  199. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  200. /// {
  201. /// //Switch to start/reset case
  202. /// m_MoveDirection = MoveDirection.None;
  203. /// }
  204. ///
  205. /// //Press the Left button to switch the Rigidbody direction to the left
  206. /// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Left"))
  207. /// {
  208. /// //Switch to the left direction
  209. /// m_MoveDirection = MoveDirection.Left;
  210. /// }
  211. /// }
  212. /// }
  213. /// </code>
  214. /// </example>
  215. Left,
  216. /// <summary>
  217. /// This is the Up state of MoveDirection. Assign functionality for moving in an upward direction.
  218. /// </summary>
  219. /// <remarks>
  220. /// 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.
  221. /// </remarks>
  222. /// <example>
  223. /// <code>
  224. /// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Up" button in Game view to see it in action.
  225. ///
  226. /// using UnityEngine;
  227. /// using UnityEngine.EventSystems;
  228. ///
  229. /// public class Example : MonoBehaviour
  230. /// {
  231. /// Vector3 m_StartPosition, m_StartForce;
  232. /// Rigidbody m_Rigidbody;
  233. /// //Use Enum for easy switching between direction states
  234. /// MoveDirection m_MoveDirection;
  235. ///
  236. /// //Use these Vectors for moving Rigidbody components
  237. /// Vector3 m_ResetVector;
  238. /// Vector3 m_UpVector;
  239. /// const float speed = 10.0f;
  240. ///
  241. /// void Start()
  242. /// {
  243. /// //You get the Rigidbody component attached to the GameObject
  244. /// m_Rigidbody = GetComponent<Rigidbody>();
  245. /// //This starts with the Rigidbody not moving in any direction at all
  246. /// m_MoveDirection = MoveDirection.None;
  247. ///
  248. /// //These are the GameObject’s starting position and Rigidbody position
  249. /// m_StartPosition = transform.position;
  250. /// m_StartForce = m_Rigidbody.transform.position;
  251. ///
  252. /// //This Vector is set to 1 in the y axis (for moving upwards)
  253. /// m_UpVector = Vector3.up;
  254. /// //This Vector is zeroed out for when the Rigidbody should not move
  255. /// m_ResetVector = Vector3.zero;
  256. /// }
  257. ///
  258. /// void Update()
  259. /// {
  260. /// //This switches the direction depending on button presses
  261. /// switch (m_MoveDirection)
  262. /// {
  263. /// //The starting state which resets the object
  264. /// case MoveDirection.None:
  265. /// //Reset to the starting position of the GameObject and Rigidbody
  266. /// transform.position = m_StartPosition;
  267. /// m_Rigidbody.transform.position = m_StartForce;
  268. /// //This resets the velocity of the Rigidbody
  269. /// m_Rigidbody.velocity = m_ResetVector;
  270. /// break;
  271. ///
  272. /// //This is for moving in an upwards direction
  273. /// case MoveDirection.Up:
  274. /// //Change the velocity so that the Rigidbody travels upwards
  275. /// m_Rigidbody.velocity = m_UpVector * speed;
  276. /// break;
  277. /// }
  278. /// }
  279. ///
  280. /// void OnGUI()
  281. /// {
  282. /// //Press the reset Button to switch to no mode
  283. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  284. /// {
  285. /// //Switch to start/reset case
  286. /// m_MoveDirection = MoveDirection.None;
  287. /// }
  288. ///
  289. /// //Press the Up button to switch the Rigidbody direction to upwards
  290. /// if (GUI.Button(new Rect(100, 60, 150, 30), "Move Up"))
  291. /// {
  292. /// //Switch to Up Direction
  293. /// m_MoveDirection = MoveDirection.Up;
  294. /// }
  295. /// }
  296. /// }
  297. /// </code>
  298. /// </example>
  299. Up,
  300. /// <summary>
  301. /// This is the Right state of MoveDirection. Assign functionality for moving to the right.
  302. /// </summary>
  303. /// <remarks>
  304. /// 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.
  305. /// </remarks>
  306. /// <example>
  307. /// <code>
  308. /// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Right" button in Game view to see it in action.
  309. ///
  310. /// using UnityEngine;
  311. /// using UnityEngine.EventSystems;
  312. ///
  313. /// public class MoveDirectionExample : MonoBehaviour
  314. /// {
  315. /// Vector3 m_StartPosition, m_StartForce;
  316. /// Rigidbody m_Rigidbody;
  317. /// //Use Enum for easy switching between direction states
  318. /// MoveDirection m_MoveDirection;
  319. ///
  320. /// //Use these Vectors for moving Rigidbody components
  321. /// Vector3 m_ResetVector;
  322. /// Vector3 m_RightVector;
  323. /// const float speed = 5.0f;
  324. ///
  325. /// void Start()
  326. /// {
  327. /// //You get the Rigidbody component attached to the GameObject
  328. /// m_Rigidbody = GetComponent<Rigidbody>();
  329. /// //This starts with the Rigidbody not moving in any direction at all
  330. /// m_MoveDirection = MoveDirection.None;
  331. ///
  332. /// //These are the GameObject’s starting position and Rigidbody position
  333. /// m_StartPosition = transform.position;
  334. /// m_StartForce = m_Rigidbody.transform.position;
  335. ///
  336. /// //This Vector is set to 1 in the x axis (for moving in the right direction)
  337. /// m_RightVector = Vector3.right;
  338. /// //This Vector is zeroed out for when the Rigidbody should not move
  339. /// m_ResetVector = Vector3.zero;
  340. /// }
  341. ///
  342. /// void Update()
  343. /// {
  344. /// //This switches the direction depending on button presses
  345. /// switch (m_MoveDirection)
  346. /// {
  347. /// //The starting state which resets the object
  348. /// case MoveDirection.None:
  349. /// //Reset to the starting position of the GameObject and Rigidbody
  350. /// transform.position = m_StartPosition;
  351. /// m_Rigidbody.transform.position = m_StartForce;
  352. /// //This resets the velocity of the Rigidbody
  353. /// m_Rigidbody.velocity = m_ResetVector;
  354. /// break;
  355. ///
  356. /// //This is for moving right
  357. /// case MoveDirection.Right:
  358. /// //This moves the Rigidbody to the right
  359. /// m_Rigidbody.velocity = m_RightVector * speed;
  360. /// break;
  361. /// }
  362. /// }
  363. ///
  364. /// void OnGUI()
  365. /// {
  366. /// //Press the reset Button to switch to no mode
  367. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  368. /// {
  369. /// //Switch to start/reset case
  370. /// m_MoveDirection = MoveDirection.None;
  371. /// }
  372. ///
  373. /// //Press the Left button to switch the Rigidbody direction to the right
  374. /// if (GUI.Button(new Rect(100, 30, 150, 30), "Move Right"))
  375. /// {
  376. /// //Switch to the left direction
  377. /// m_MoveDirection = MoveDirection.Right;
  378. /// }
  379. /// }
  380. /// }
  381. /// </code>
  382. /// </example>
  383. Right,
  384. /// <summary>
  385. /// The Down State of MoveDirection. Assign functionality for moving in a downward direction.
  386. /// </summary>
  387. /// <remarks>
  388. /// 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.
  389. /// </remarks>
  390. /// <example>
  391. /// <code>
  392. /// //Attach this script to a GameObject with a Rigidbody component. Press the "Move Down" button in Game view to see it in action.
  393. ///
  394. /// using UnityEngine;
  395. /// using UnityEngine.EventSystems;
  396. ///
  397. /// public class Example : MonoBehaviour
  398. /// {
  399. /// Vector3 m_StartPosition, m_StartForce;
  400. /// Rigidbody m_Rigidbody;
  401. /// //Use Enum for easy switching between direction states
  402. /// MoveDirection m_MoveDirection;
  403. ///
  404. /// //Use these Vectors for moving Rigidbody components
  405. /// Vector3 m_ResetVector;
  406. /// Vector3 m_UpVector;
  407. /// const float speed = 10.0f;
  408. ///
  409. /// void Start()
  410. /// {
  411. /// //You get the Rigidbody component attached to the GameObject
  412. /// m_Rigidbody = GetComponent<Rigidbody>();
  413. /// //This starts with the Rigidbody not moving in any direction at all
  414. /// m_MoveDirection = MoveDirection.None;
  415. ///
  416. /// //These are the GameObject’s starting position and Rigidbody position
  417. /// m_StartPosition = transform.position;
  418. /// m_StartForce = m_Rigidbody.transform.position;
  419. ///
  420. /// //This Vector is set to 1 in the y axis (for moving upwards)
  421. /// m_UpVector = Vector3.up;
  422. /// //This Vector is zeroed out for when the Rigidbody should not move
  423. /// m_ResetVector = Vector3.zero;
  424. /// }
  425. ///
  426. /// void Update()
  427. /// {
  428. /// //This switches the direction depending on button presses
  429. /// switch (m_MoveDirection)
  430. /// {
  431. /// //The starting state which resets the object
  432. /// case MoveDirection.None:
  433. /// //Reset to the starting position of the GameObject and Rigidbody
  434. /// transform.position = m_StartPosition;
  435. /// m_Rigidbody.transform.position = m_StartForce;
  436. /// //This resets the velocity of the Rigidbody
  437. /// m_Rigidbody.velocity = m_ResetVector;
  438. /// break;
  439. ///
  440. /// //This is for moving down
  441. /// case MoveDirection.Down:
  442. /// //This moves the Rigidbody down
  443. /// m_Rigidbody.velocity = -m_UpVector * speed;
  444. /// break;
  445. /// }
  446. /// }
  447. ///
  448. /// void OnGUI()
  449. /// {
  450. /// //Press the reset Button to switch to no mode
  451. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  452. /// {
  453. /// //Switch to start/reset case
  454. /// m_MoveDirection = MoveDirection.None;
  455. /// }
  456. ///
  457. /// //Press the Down button to switch the direction to down
  458. /// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
  459. /// {
  460. /// //Switch to Down Direction
  461. /// m_MoveDirection = MoveDirection.Down;
  462. /// }
  463. /// }
  464. /// }
  465. /// </code>
  466. /// </example>
  467. Down,
  468. /// <summary>
  469. /// This is the None state. Assign functionality that stops movement.
  470. /// </summary>
  471. /// <remarks>
  472. /// 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.
  473. /// </remarks>
  474. /// <example>
  475. /// <code>
  476. /// //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.
  477. ///
  478. /// using UnityEngine;
  479. /// using UnityEngine.EventSystems;
  480. ///
  481. /// public class Example : MonoBehaviour
  482. /// {
  483. /// Vector3 m_StartPosition, m_StartForce;
  484. /// Rigidbody m_Rigidbody;
  485. /// //Use Enum for easy switching between direction states
  486. /// MoveDirection m_MoveDirection;
  487. ///
  488. /// //Use these Vectors for moving Rigidbody components
  489. /// Vector3 m_ResetVector;
  490. /// Vector3 m_UpVector;
  491. /// const float speed = 10.0f;
  492. ///
  493. /// void Start()
  494. /// {
  495. /// //You get the Rigidbody component attached to the GameObject
  496. /// m_Rigidbody = GetComponent<Rigidbody>();
  497. /// //This starts with the Rigidbody not moving in any direction at all
  498. /// m_MoveDirection = MoveDirection.None;
  499. ///
  500. /// //These are the GameObject’s starting position and Rigidbody position
  501. /// m_StartPosition = transform.position;
  502. /// m_StartForce = m_Rigidbody.transform.position;
  503. ///
  504. /// //This Vector is set to 1 in the y axis (for moving upwards)
  505. /// m_UpVector = Vector3.up;
  506. /// //This Vector is zeroed out for when the Rigidbody should not move
  507. /// m_ResetVector = Vector3.zero;
  508. /// }
  509. ///
  510. /// void Update()
  511. /// {
  512. /// //This switches the direction depending on button presses
  513. /// switch (m_MoveDirection)
  514. /// {
  515. /// //The starting state which resets the object
  516. /// case MoveDirection.None:
  517. /// //Reset to the starting position of the GameObject and Rigidbody
  518. /// transform.position = m_StartPosition;
  519. /// m_Rigidbody.transform.position = m_StartForce;
  520. /// //This resets the velocity of the Rigidbody
  521. /// m_Rigidbody.velocity = m_ResetVector;
  522. /// break;
  523. ///
  524. /// //This is for moving down
  525. /// case MoveDirection.Down:
  526. /// //This moves the Rigidbody down
  527. /// m_Rigidbody.velocity = -m_UpVector * speed;
  528. /// break;
  529. /// }
  530. /// }
  531. ///
  532. /// void OnGUI()
  533. /// {
  534. /// //Press the reset Button to switch to no mode
  535. /// if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
  536. /// {
  537. /// //Switch to start/reset case
  538. /// m_MoveDirection = MoveDirection.None;
  539. /// }
  540. ///
  541. /// //Press the Down button to switch the direction to down
  542. /// if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down"))
  543. /// {
  544. /// //Switch to Down Direction
  545. /// m_MoveDirection = MoveDirection.Down;
  546. /// }
  547. /// }
  548. /// }
  549. /// </code>
  550. /// </example>
  551. None
  552. }
  553. }