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);
}

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?

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();
	}
}