- Home /
How to play sound on trigger enter a distance behind the player?
Hey,
How would I go about making it so that when the player is within the trigger a sound is played X distance behind the player? This next bit may be difficult; the particular part I want to include this in, is in a level that takes place in long streets. Is there any way to make it so that the sound is only played behind the player but within the confines of the street?
The only way i can think to do so is to attach the sound to an invisible NPC that follows the player but cannot go through walls. Would that work?
Thanks.
Answer by aldonaletto · Jul 26, 2012 at 10:49 AM
The "invisible NPC" alternative may cause problems with shots, explosions or other moving objects because the NPC collider will block them.
If the trigger is static, you can use its position/orientation to define the sound origin, and play the sound at that point with AudioSource.PlayClipAtPoint: this function creates a temporary game object with an AudioSource at the specified point, plays the sound and destroys the object automatically. The trigger volume orientation will define the sound direction: if the player enters its front side, the trigger's forward direction will be used; if entering the back side, use the backward direction (trigger script):
var sound: AudioClip; var distance: float = 30;
function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ var tPlayer = other.transform; // find the direction trigger->player: var dirPlayer = tPlayer.position - transform.position; // get the absolute angle from the trigger forward direction: var angle = Vector3.Angle(dirPlayer, transform.forward); if (angle < 90){ dirPlayer = transform.forward; // player in front of the trigger } else { dirPlayer = -transform.forward; // player behind the trigger } // calculate the point at "distance" behind the player: var point = tPlayer.position + dirPlayer * distance; AudioSource.PlayClipAtPoint(sound, point); } }
Another possibility is to define the sound origin using the player's forward direction, then check if there are no obstacles in between with Linecast (trigger script):
var sound: AudioClip; var distance: float = 30;
function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ var tPlayer = other.transform; // calculate the point at "distance" behind the player: var point = tPlayer.position - tPlayer.forward * distance; // check if there are no obstacles in between: if (!Physics.Linecast(tPlayer.position, point)){ // free line of sight: play the sound there: AudioSource.PlayClipAtPoint(sound, point); } } }
Will this manage to stay within the street? Also, due to it being a (helpless)horror game there is no problem with guns, ect. Basically the idea is that as you walk down the street all will be quiet, then as you walk into a certain section of streets (with the trigger enveloping them all) you will hear footsteps behind you and if you turn around 3 times then the enemy is 'allowed' to attack. I thought the npc solution may work because I will have to display the model behind the player at the audio source on the third turn around and doing so will give me the game object to attach the model to.
Well, in this case the NPC is a better alternative, since you will need it to attack the player anyway. Child the model to it, and set its renderer.enabled property to false to make it invisible. Use OnTriggerEnter/Exit to enable/disable the sound, like this (trigger script - let's call it TriggerScript.js):
var enableSound = false;
function OnTriggerEnter(other: Collider){ if (other.tag == "Player") enableSound = true; }
function OnTriggerExit(other: Collider){ if (other.tag == "Player") enableSound = false; } Enemy script:
var trigger: TriggerScript; // drag the trigger from Hierarchy to here var footsteps: AudioClip[]; // populate this array with footsteps var stepSize: float = 0.7; private lastStep: Vector3;
function Update(){ if (trigger.enableSound){ var dist = transform.position - lastStep; if (dist.magnitude >= stepSize){ audio.PlayOneShot(footsteps[Random.Range(0, footsteps.length)]); lastStep = transform.position; } } ...
Thanks for your help, ill do my best to implement it :S lol
Ah! By using rendered.enabled = false will that make if(renderer.isVisible) return false too? Because the turning around uses that to detect the NPC
Good point - isVisible doesn't work when the renderer is disabled. An alternative could be to create a completely transparent material (use Transparent/Diffuse shader and set the $$anonymous$$ain Color Alpha component to zero) and swap it with the original material when needed (model script):
var visible = false; // controls the visibility var invisible$$anonymous$$at: $$anonymous$$aterial; // drag the invisible material here private var lastVis = true; private var original$$anonymous$$at: $$anonymous$$aterial;
function Start(){ original$$anonymous$$at = renderer.material; }
function Update(){ if (visible != lastVis){ if (visible){ renderer.material = original$$anonymous$$at; } else { renderer.material = invisible$$anonymous$$at; } } }