I am using the FMOD Event Manager in Unity and need to send it a Vector3 about where I want a sound to play. Using this I want to create a sound from a wall when the player hits it.
I basically want to get the point of contact each time the player hits the wall so that the sound will play from there. I’ve been through the Unity script reference and so far I can’t seem to get ContactPoint to do this. Could anyone suggest a way of getting a script attached to a wall to give me the position of the player’s point of contact each time it happens?
Add this script to the wall and it will produce the sound wallSound at the contact position:
var wallSound: AudioClip;
function OnCollisionEnter(col:Collision){
if (col.contacts.Length>0){
AudioSource.PlayClipAtPoint(wallSound, col.contacts[0].point);
}
}
EDITED: I don’t know much about FMOD, thus I can’t tell how to pass the point to it, but the basic idea still applies - you can get the collision point from the contacts array in OnCollisionEnter. But if the character is a Character Controller, it will not work - you should use OnControllerCollisionHit and get the point directly from the ControllerCollisionHit structure - but there’s a problem: the Character Controller reports collisions with the ground all the time, so you must filter the normals returned to only accept those almost horizontal, like below:
function OnControllerColliderHit(col:ControllerColliderHit){
if (Mathf.Abs(col.normal.y)<0.2){ // accept only almost horizontal collisions
if (col.collider.tag == "Wall"){ // and only from objects tagged "Wall"
var point: Vector3 = col.point;
// do whatever you want with the collision point
}
}
}