- Home /
Audio Plays when close to player
Hello. I Have got a Zombie and an AI Script. But i would like an audio/sound effect to play when it's in a certain about of Meters away from the Player, because all it does it keep repeating or only play once when i click play. I've made a Sound Effect, so all i need it the Code, it would help alot. Thanks!
Swifted
Answer by aldonaletto · Sep 18, 2012 at 04:58 PM
You can measure the distance to the player, and play the sound when the player is close enough. Modify the Update function in your AI script like this (supposing target is a GameObject variable that references the player):
... var soundRange: float = 15; // range in meters
function Update(){ // include this code in your current Update function: var dist = Vector3.Distance(target.transform.position, transform.position); if (dist <= soundRange && audio.isPlaying == false){ audio.Play(); } // original Update code // ...
Your answer
Follow this Question
Related Questions
How to use external audio file in Android application? ,How to read external audio files on Android? 0 Answers
Is it possible to run another program from a Unity executable? 2 Answers
Follow Player when Close C# Help 1 Answer
Random FPS player Audio when player kills enemies 4 Answers
can i start playing from a specefic part of an audio clip ? 1 Answer