- Home /
Collider isTrigger for audio clip lets me pass through object. How to fix?
Hi
So I have a barricade and a 1st Person Controller. (OOTB unity version).
When my player collides with the barrier I want to play a sound to indicate it hurt the player. (This is just a test level to practice if you were wondering why)
On the 1st person controller "Character Motor" script, I added the following:
// Require an audioclip to be assigned
@script RequireComponent(AudioSource)
var collision : AudioClip;
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Barricade"){
AudioSource.PlayClipAtPoint(collision, transform.position);
}
}
However, when I click the isTrigger checkbox on the Barricade's box collider, it then allows me to pass through the Barricade whilst playing the sound. If isTrigger is left unchecked, it stops me passing through but it doesnt play the sound as expected.
I want it to still stop me from passing through but play the sound aswell when I hit it.
Can anyone spot where I'm going wrong or offer some advice please?
Thanks in advance, een
Have you try OnCollisionEnter?
@script RequireComponent(AudioSource);
var collision : AudioClip;
function OnCollisionEnter(coll: Collision)
{
if(coll.gameObject.tag == "Barricade")
{
AudioSource.PlayClipAtPoint(collision, transform.position);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085335im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnTriggerExit stop audio 1 Answer
Sound plays right at the beginning 2 Answers
Audio Loops? 1 Answer
Find all Objects/Scripts that access a given audio!! 1 Answer
Assign a clip to an AudioSource when it finishes playing. 1 Answer