Audio Plays when close to player

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.


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){
    // original Update code
    // ...