- Home /
play sound at Impact Point
I want to play an audio source once at the point of impact for the player in 3D space, but also loop it when sliding against something. Here is what I have so far, but it plays on every single contact hit and sounds very bad. I'm using a CharacterController.
function OnControllerColliderHit(hit : ControllerColliderHit) {
//exit if we hit the ground
if (hit.moveDirection.y < -0.3)
return;
audio.PlayClipAtPoint(impactSound, hit.point);
}
Answer by IsaiahKelly · Nov 24, 2012 at 09:26 PM
I solved the problem by creating a audio emitter object in the scene that moves to the player's last hit location and loops.
function Start() {
//first find the audio impact object
soundPoint = GameObject.Find("AudioHitPoint");
}
function OnControllerColliderHit(hit : ControllerColliderHit) {
//exit if hitting the ground
if (hit.moveDirection.y < -0.3)
return;
//move the audio impact object to hit location
soundPoint.transform.position = hit.point;
//then loop the sound when we are hitting something
if (!soundPoint.audio.isPlaying){
soundPoint.audio.clip = impactSound;
soundPoint.audio.Play();
}
}
Answer by Montraydavis · Nov 15, 2012 at 02:25 PM
I don't see where you check for a specific object colliding, and also, you need an OnCollisionStay.
This may not be what you need 100%, but, is a starter;
var HitPoint : Vector3 ;
function OnControllerColliderHit(hit : ControllerColliderHit) {
HitPoint = hit.point;
//exit if we hit the ground
if (hit.moveDirection.y < -0.3)
return;
if ( hit.collider.tag == "TagYouWantToCheckAgainst" ){
audio.PlayClipAtPoint(impactSound, hit.point);
}
}
function OnCollisionStay ( collision : Collision)
{
if ( collision.collider.tag == "TagYouWantToCheckAgainst" && !audio.IsPlaying())
{
audio.PlayClipAtPoint(impactSound, HitPoint);
}
}
As far as it "sounding bad", we need a bit more info as to what might be causing this. Does the audio file sound normal when played in iTunes or Windows Media Player, etc?
Checking for a specific object isn't a problem and not part of the question. Also audio.IsPlaying doesn't work with audio.PlayClipAtPoint. And OnCollisionStay doesn't work with my CharacterController for some reason. I said “it plays on every single contact hit” and that's why it sounds bad.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083916im_/https://answers.unity.com/themes/thub/images/avi.jpg)