When my character controller is pushing a crate, the pushing script calls the PlayPushingSound() function on the crate to play a dragging sound… But how can I tell it when I’ve stopped pushing the crate, thus ceasing the sound playback?
I could just add an update function that’s always running on the crate script but seems kinda inefficient. Is there a better way?
(Simplified):
function OnControllerColliderHit (hit : ControllerColliderHit) {
var body : Rigidbody = hit.collider.attachedRigidbody;
// Calculate push direction from move direction, we only push objects to the sides
// never up and down
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// push with move speed but never more than half runspeed
body.velocity = pushDir * pushPower * Mathf.Min (controller.GetSpeed (), controller.movement.runSpeed/2);
hit.gameObject.GetComponent(PushingSoundEffect).PushingSound();
}
Script attached to crate:
function PushingSound() {
// Don't restart audio clip
if (audio.isPlaying)
return;
audio.Play();
}
@script RequireComponent(AudioSource)