Game Development Reference
In-Depth Information
public class DroidScript : MonoBehaviour, IAlarmHandler {
public void OnAlarmTrigger(AlarmEventData eventData)
{
//Intruder found, attack!!!
}
}
So we have our event template, to this we'll add a simple
Coroutine
(after the
OnAlarmTrigger
function) that will make our cubes race to the alarm spot once it
triggers, as shown in the following code (mainly to have something visible to see,
I would have preferred turrets shooting at their foe but that would have been a lot
more to script):
//Coroutine that will move an object from its current
//position to another
IEnumerator MoveToPoint(Vector3 target)
{
float timer = 0f;
var StartPosition = transform.position;
while (target != transform.position)
{
transform.position =
Vector3.Lerp(StartPosition, target, timer);
timer += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
yield return null;
}
Next, we update the
OnAlarmTrigger
function for the interface, to call
Coroutine
when it's called:
public void OnAlarmTrigger(AlarmEventData eventData)
{
//Intruder found, attack!!!
StartCoroutine(MoveToPoint(eventData.AlarmTriggerData));
}
To finish off, add the
DroidScript
script to the
Pickup Prefab
(select the
Pickup
prefab
, click
Add
Component in the inspector, and select the script) and the rolling
cubes of death will be ready.