2
0

BalloonColliders.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Can be attached to the controller to collide with the balloons
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class BalloonColliders : MonoBehaviour
  12. {
  13. public GameObject[] colliders;
  14. private Vector3[] colliderLocalPositions;
  15. private Quaternion[] colliderLocalRotations;
  16. private Rigidbody rb;
  17. //-------------------------------------------------
  18. void Awake()
  19. {
  20. rb = GetComponent<Rigidbody>();
  21. colliderLocalPositions = new Vector3[colliders.Length];
  22. colliderLocalRotations = new Quaternion[colliders.Length];
  23. for ( int i = 0; i < colliders.Length; ++i )
  24. {
  25. colliderLocalPositions[i] = colliders[i].transform.localPosition;
  26. colliderLocalRotations[i] = colliders[i].transform.localRotation;
  27. colliders[i].name = gameObject.name + "." + colliders[i].name;
  28. }
  29. }
  30. //-------------------------------------------------
  31. void OnEnable()
  32. {
  33. for ( int i = 0; i < colliders.Length; ++i )
  34. {
  35. colliders[i].transform.SetParent( transform );
  36. colliders[i].transform.localPosition = colliderLocalPositions[i];
  37. colliders[i].transform.localRotation = colliderLocalRotations[i];
  38. colliders[i].transform.SetParent( null );
  39. FixedJoint fixedJoint = colliders[i].AddComponent<FixedJoint>();
  40. fixedJoint.connectedBody = rb;
  41. fixedJoint.breakForce = Mathf.Infinity;
  42. fixedJoint.breakTorque = Mathf.Infinity;
  43. fixedJoint.enableCollision = false;
  44. fixedJoint.enablePreprocessing = true;
  45. colliders[i].SetActive( true );
  46. }
  47. }
  48. //-------------------------------------------------
  49. void OnDisable()
  50. {
  51. for ( int i = 0; i < colliders.Length; ++i )
  52. {
  53. if ( colliders[i] != null )
  54. {
  55. Destroy( colliders[i].GetComponent<FixedJoint>() );
  56. colliders[i].SetActive( false );
  57. }
  58. }
  59. }
  60. //-------------------------------------------------
  61. void OnDestroy()
  62. {
  63. for ( int i = 0; i < colliders.Length; ++i )
  64. {
  65. Destroy( colliders[i] );
  66. }
  67. }
  68. }
  69. }