No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MoveDirection.cs 25KB

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