Assignment for RMIT Mixed Reality in 2020
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.

87 lines
2.2 KiB

  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. }