I’ve seen a lot of scripts that play a sound once you collide with an object, but I need one that makes a sound play once you come close to a specific object. I also need it to work on multiple objects throughout one level. Can someone help me with a script?
You can compare the distance between the player and the object with distance() function and then when the guy gets in the range, play the sound.
var other : Transform;
var range:int;
var sound:AudioClip;
var played:boolean;
var timing:int;
var delay:int=10;
function Start(){
played=false;
timing =Time.time;}
function Update(){
if (Vector3.Distance(other.position, transform.position)<range) {
if(timing<Time.time){
played = true;
timing = Time.time + delay;}
}
if(played){
AudioSource.PlayClipAtPoint(sound, transform.position);
played=false;
}