subere23

TransformMenuMovement

Sep 28th, 2017
544
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. bool initialSetupComplete = true;
  10.  
  11. void OnEnable () {
  12.  
  13.  
  14. NRSRManager.ObjectFocused += Enabled_GetPosition;
  15. NRSRManager.ObjectUnFocused += Disabled_Reset;
  16.  
  17. toolBounds = GetBoundsForAllChildren(gameObject);
  18. cursor = GameObject.Find("Cursor");
  19. }
  20.  
  21. private void OnDisable()
  22. {
  23. NRSRManager.ObjectFocused -= Enabled_GetPosition;
  24. NRSRManager.ObjectUnFocused -= Disabled_Reset;
  25. }
  26.  
  27. void Disabled_Reset()
  28. {
  29.  
  30. initialSetupComplete = true;
  31. }
  32.  
  33. void Enabled_GetPosition()
  34. {
  35. if (initialSetupComplete)
  36. {
  37. transform.position = cursor.transform.position;
  38. transform.rotation = cursor.transform.rotation * Quaternion.Euler(0, 0, 180);
  39. initialSetupComplete = false;
  40.  
  41. }
  42. }
  43.  
  44. void Update () {
  45.  
  46. if(transform.localPosition.x - cursor.transform.localPosition.x > toolBounds.extents.x / 3 ||
  47. transform.localPosition.x - cursor.transform.localPosition.x < -toolBounds.extents.x / 3 ||
  48. transform.localPosition.y - cursor.transform.localPosition.y > toolBounds.extents.y /3 ||
  49. transform.localPosition.y - cursor.transform.localPosition.y < - toolBounds.extents.y /3)
  50. {
  51. transform.position = Vector3.Lerp(transform.position, cursor.transform.position, 0.02f);
  52. }
  53.  
  54. transform.rotation = Quaternion.Lerp(transform.rotation,
  55. cursor.transform.rotation * Quaternion.Euler(0, 0, 180),
  56. 1);
  57.  
  58. transform.position = new Vector3(transform.position.x,
  59. transform.position.y,
  60. cursor.transform.position.z - 0.1f);
  61.  
  62. }
  63.  
  64. public Bounds GetBoundsForAllChildren(GameObject findMyBounds)
  65. {
  66. Bounds result = new Bounds(Vector3.zero, Vector3.zero);
  67.  
  68. foreach (Collider coll in findMyBounds.GetComponentsInChildren<Collider>())
  69. {
  70. if (result.extents == Vector3.zero)
  71. {
  72. result = coll.bounds;
  73. }
  74. else
  75. {
  76. result.Encapsulate(coll.bounds);
  77. }
  78. }
  79.  
  80. return result;
  81. }
  82.  
  83. }
RAW Paste Data