I’m doing a sound design overhaul of the Angry Bots demo. I’m stumped adding an audio behavior to the elevator. I have a toggle that starts the sounds when the player enters the elevator and two existing colliders that trigger the elevator to go down or up. I want the “up/down” colliders to stop the elevator audio. I have the script for the audio all setup in the elevator object but I can’t make the child object “ElevatorPlate” trigger the Up/Down box colliders. I tried modifying an existing module in the get “TriggerOnTag” to no avail. I’m a scripting noob and I know doing something fundamentally wrong. This is the last thing I need to do and any help much appreciated!!
#pragma strict
public var triggerTag : String = "ElevatorPlate";
public var enterSignals : SignalSender;
public var exitSignals : SignalSender;
function OnCollisionEnter (collision : Collider) {
if (collision.isTrigger)
return;
if (collision.gameObject.tag == triggerTag || triggerTag == "") {
enterSignals.SendSignals (this);
}
}
function OnCollisionExit (collision : Collider) {
if (collision.isTrigger)
return;
if (collision.gameObject.tag == triggerTag || triggerTag == "") {
exitSignals.SendSignals (this);
}
}
You’re messing triggers and colliders. For triggers, you must use OnTrigger events; for regular colliders, use OnCollision events. Only events of the correct type are generated, thus you don’t have to check whether it’s a trigger or a collider. OnTrigger events pass the other collider, while OnCollision events pass a Collision structure. Since you’re apparently discarding triggers, I suppose that a rigidbody is hitting a regular collider, thus OnCollision events should be used:
public var triggerTag : String = "ElevatorPlate";
public var enterSignals : SignalSender;
public var exitSignals : SignalSender;
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.tag == triggerTag || triggerTag == "") {
enterSignals.SendSignals (this);
}
}
function OnCollisionExit (collision : Collision) {
if (collision.gameObject.tag == triggerTag || triggerTag == "") {
exitSignals.SendSignals (this);
}
}
For this script to work, it must be attached to a rigidbody that hits an object tagged as “ElevatorPlate” (or not tagged at all). OnCollision and OnTrigger events are sent to scripts attached to both objects that collided, but each object receives info about the other object. Another important point: the rigidbody must be moving in order to generate OnCollision/OnTrigger events - a still rigidbody enters sleep mode, and nothing happens when a collider hits it.