-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathCollisionCallbacks.cs
141 lines (124 loc) · 5.01 KB
/
CollisionCallbacks.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using UnityEngine;
using UnityEngine.Events;
namespace Unity.MLAgentsExamples
{
/// <summary>
/// Utility class to allow target placement and collision detection with an agent
/// Add this script to the target you want the agent to touch.
/// Callbacks will be triggered any time the target is touched with a collider tagged as 'tagToDetect'
/// </summary>
public class CollisionCallbacks : MonoBehaviour
{
// [System.Serializable] public class BoolEvent : UnityEvent<bool> { }
// [SerializeField] BoolEvent boolEvent = new BoolEvent();
// public void OnBoolEvent(bool value)
// {
// Debug.Log($"OnBoolEvent {value}");
// }
[Header("Collider Tag To Detect")]
public string tagToDetect = "agent"; //collider tag to detect
// [Header("Target Placement")]
// public float spawnRadius; //The radius in which a target can be randomly spawned.
// public bool respawnIfTouched; //Should the target respawn to a different position when touched
//
// [Header("Target Fell Protection")]
// public bool respawnIfFallsOffPlatform = true; //If the target falls off the platform, reset the position.
// public float fallDistance = 5; //distance below the starting height that will trigger a respawn
//
//
// private Vector3 m_startingPos; //the starting position of the target
// private Agent m_agentTouching; //the agent currently touching the target
[System.Serializable]
// public class TriggerEvent : UnityEvent<string>
public class TriggerEvent : UnityEvent<Collider>
{
}
[Header("Trigger Callbacks")]
public TriggerEvent onTriggerEnterEvent = new TriggerEvent();
public TriggerEvent onTriggerStayEvent = new TriggerEvent();
public TriggerEvent onTriggerExitEvent = new TriggerEvent();
[System.Serializable]
public class CollisionEvent : UnityEvent<Collision, Transform>
{
}
[Header("Collision Callbacks")]
public CollisionEvent onCollisionEnterEvent = new CollisionEvent();
public CollisionEvent onCollisionStayEvent = new CollisionEvent();
public CollisionEvent onCollisionExitEvent = new CollisionEvent();
// // Start is called before the first frame update
// void OnEnable()
// {
// m_startingPos = transform.position;
// if (respawnIfTouched)
// {
// MoveTargetToRandomPosition();
// }
// }
// void Update()
// {
// if (respawnIfFallsOffPlatform)
// {
// if (transform.position.y < m_startingPos.y - fallDistance)
// {
// Debug.Log($"{transform.name} Fell Off Platform");
// MoveTargetToRandomPosition();
// }
// }
// }
// /// <summary>
// /// Moves target to a random position within specified radius.
// /// </summary>
// public void MoveTargetToRandomPosition()
// {
// var newTargetPos = m_startingPos + (Random.insideUnitSphere * spawnRadius);
// newTargetPos.y = m_startingPos.y;
// transform.position = newTargetPos;
// }
private void OnCollisionEnter(Collision col)
{
if (col.transform.CompareTag(tagToDetect))
{
onCollisionEnterEvent.Invoke(col, transform);
// if (respawnIfTouched)
// {
// MoveTargetToRandomPosition();
// }
}
}
private void OnCollisionStay(Collision col)
{
if (col.transform.CompareTag(tagToDetect))
{
onCollisionStayEvent.Invoke(col, transform);
}
}
private void OnCollisionExit(Collision col)
{
if (col.transform.CompareTag(tagToDetect))
{
onCollisionExitEvent.Invoke(col, transform);
}
}
private void OnTriggerEnter(Collider col)
{
if (col.CompareTag(tagToDetect))
{
onTriggerEnterEvent.Invoke(col);
}
}
private void OnTriggerStay(Collider col)
{
if (col.CompareTag(tagToDetect))
{
onTriggerStayEvent.Invoke(col);
}
}
private void OnTriggerExit(Collider col)
{
if (col.CompareTag(tagToDetect))
{
onTriggerExitEvent.Invoke(col);
}
}
}
}