-
Notifications
You must be signed in to change notification settings - Fork 3
/
CollisionState.js
60 lines (51 loc) · 1.48 KB
/
CollisionState.js
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
//----------------------------------------
// A component that records and manages which colliders are currently colliding
// with the object.
//----------------------------------------
#pragma strict
var debugNumCols = false;
var debugColor = Color.red;
var debugDrawSecs = 0.1;
var debugContacts = false;
var minNormalY:float = -9999;
// Stores collider -> time of enter
private var col2expire = new Dictionary.<Collider, float>();
function OnCollisionEnter( col : Collision ) : void
{
OnCollisionStay( col );
}
function OnCollisionStay( col : Collision ) : void
{
for( var c : ContactPoint in col.contacts ) {
if( c.normal.y > minNormalY ) {
col2expire[ c.otherCollider ] = Time.time + Time.deltaTime;
if( debugContacts ) {
Debug.DrawRay( c.point, c.normal, debugColor, debugDrawSecs );
}
}
}
}
function OnCollisionExit( col : Collision ) : void
{
for( var c : ContactPoint in col.contacts ) {
if( col2expire.ContainsKey( c.otherCollider ) )
col2expire.Remove( c.otherCollider );
}
}
function Update()
{
if( debugNumCols ) {
Debug.Log('num cols on ' + gameObject.name + ' = ' + col2expire.Count );
}
// we need to "expire" collisions, because if colliders are destroyed,
// OnCollisionExit doesn't fire...
// we have to do it this way...for dumb reasons...
var keys = new Array();
for( var key in col2expire.Keys ) keys.Push(key);
for( var col in keys ) {
var expireTime = col2expire[ col ];
if( expireTime < Time.time ) {
col2expire.Remove( col );
}
}
}