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