SwitchTrigger
/**
* Add this script to a GameObject with a trigger collider.
* Sends information to the trigger target about the trigger status.
* Also plays on/off animation for trigger if available.
**/
using UnityEngine;
using System.Collections;
public class SwitchTrigger : MonoBehaviour {
public SwitchTriggerTarget triggerObject;
public AnimationClip onClip;
public AnimationClip offClip;
private int cnt;
void Awake() {
cnt = 0;
if (onClip)
GetComponent<Animation>().AddClip(onClip, "on");
if (offClip)
GetComponent<Animation>().AddClip(offClip, "off");
}
void OnTriggerEnter(Collider other) {
cnt++;
if (cnt == 1) {
// Send trigger message to linked object.
if (triggerObject) {
triggerObject.OnTriggerSwitch(true);
} else {
Debug.Log("No trigger object attached.");
}
// Play on animation, if available.
if (onClip)
GetComponent<Animation>().Play("on");
}
}
void OnTriggerExit(Collider other) {
cnt--;
if (cnt == 0) {
// Send trigger message to linked object.
if (triggerObject)
triggerObject.OnTriggerSwitch(false);
// Play off animation, if available.
if (offClip)
GetComponent<Animation>().Play("off");
}
}
}
Recent Comments