subere23

Ch8 - TransformToolMenuMovement Update

Oct 1st, 2017
269
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TransformMenuMovement : MonoBehaviour {
  6.  
  7. Bounds toolBounds;
  8. GameObject cursor;
  9. Vector3 menuPosition;
  10. bool initialSetupComplete = true;
  11.  
  12. void OnEnable () {
  13.  
  14. NRSRManager.ObjectFocused += Enabled_GetPosition;
  15. NRSRManager.ObjectUnFocused += Disabled_Reset;
  16.  
  17. toolBounds = GetBoundsForAllChildren(gameObject);
  18. cursor = GameObject.Find("Cursor");
  19.  
  20. }
  21.  
  22. private void OnDisable()
  23. {
  24. NRSRManager.ObjectFocused -= Enabled_GetPosition;
  25. NRSRManager.ObjectUnFocused -= Disabled_Reset;
  26. }
  27.  
  28. void Disabled_Reset()
  29. {
  30.  
  31. initialSetupComplete = true;
  32. }
  33.  
  34. void Enabled_GetPosition()
  35. {
  36. if (initialSetupComplete)
  37. {
  38. transform.position = menuPosition;
  39. transform.rotation = cursor.transform.rotation * Quaternion.Euler(0, 0, 180);
  40. initialSetupComplete = false;
  41.  
  42. }
  43. }
  44.  
  45. void Update () {
  46.  
  47. //get the updated hitinfo position every frame
  48. menuPosition = NRSRManager.menuPosition;
  49.  
  50.  
  51. if (transform.localPosition.x - menuPosition.x > toolBounds.extents.x / 3 ||
  52. transform.localPosition.x - menuPosition.x < -toolBounds.extents.x / 3 ||
  53. transform.localPosition.y - menuPosition.y > toolBounds.extents.y /3 ||
  54. transform.localPosition.y - menuPosition.y < - toolBounds.extents.y /3)
  55. {
  56. transform.position = Vector3.Lerp(transform.position, menuPosition, 0.02f);
  57. }
  58.  
  59. transform.rotation = Quaternion.Lerp(transform.rotation,
  60. cursor.transform.rotation * Quaternion.Euler(0, 0, 180),
  61. 1);
  62.  
  63. transform.position = new Vector3(transform.position.x,
  64. transform.position.y,
  65. menuPosition.z - 0.1f);
  66.  
  67. }
  68.  
  69. public Bounds GetBoundsForAllChildren(GameObject findMyBounds)
  70. {
  71. Bounds result = new Bounds(Vector3.zero, Vector3.zero);
  72.  
  73. foreach (Collider coll in findMyBounds.GetComponentsInChildren<Collider>())
  74. {
  75. if (result.extents == Vector3.zero)
  76. {
  77. result = coll.bounds;
  78. }
  79. else
  80. {
  81. result.Encapsulate(coll.bounds);
  82. }
  83. }
  84.  
  85. return result;
  86. }
  87.  
  88. }
RAW Paste Data