What's the best way to do this. I have a spooky old chicken coop that has an alien zombie inside munching on a dead chicken. I want some spooky music to play when my FPS player gets close to the chicken coop to signify that there is something spooky about to happen or be discovered. Right now I'm using this script:
var spookyChickenHouse : AudioClip;
var playAudioDist = 1.0;
private var player : GameObject;
function Start(){
player = GameObject.FindWithTag("Player");
}
function Update(){
if(Vector3.Distance(transform.position, player.gameObject.transform.position) < playAudioDist){
audio.PlayOneShot(spookyChickenHouse);
}
}
It does play the music theme based on my FPS player range of getting within a certain distance from the chicken coop, only thing is, is that the music seems to play more than once which I don't want and it also seems to prevent some of the other sounds from playing like when my Alien Zombie attacks, his attack sounds get cut off.
Is there a better way of doing this?
EDIT:
Well I don't know if it's the propper way to have done this but what I ended up doing was placing a collider so it stretches across the pathway that leads to the old farm area, giving it a trigger and placing this code on it:
var spookyChickenHouse : AudioClip;
function OnTriggerEnter(col : Collider){
if(col.gameObject.tag == "Player"){
audio.clip = spookyChickenHouse;
audio.Play();
yield WaitForSeconds(70);
Destroy(gameObject);
}
}
This way my FPS player activates the spooky music and once it's finnishes playing, aproximately 70 seconds the game object destroys itself that way my player will not accidentally activate the sound if they choose to return to the farm house which is back down the path. It works, but like I say, I dunno if it's the propper way though?
Edit: New problem I just discovered. This script above is working great only I discovered that lets say my character / player hears the onset of spooky music and descides they are not yet ready to continue onwards so they turn back, and of course they collide again with my Music start collider, so what hapens is th music instantly starts over again even though it had not played through the first time.
Now is there a way to make sure that the music continues to play all the way through without getting cut off by starting over again because the player again activated the collider? Even better still to make sure that the music not only continues to play uninterupted but also still have the game object destroy itself so that way the music only plays once on collision?
I'd also recommend not constantly checking Vector3.Distance, since it's fairly expensive to use in Update. Michael, is there a reason you aren't using a collider/trigger-based system?
– burnumdI checked out the Reference but it does not seem to say how to do this. burnumd: I thought about using a collider but then I figured it would just play the sound again the next time my player walked into it, the distance range approach seemed to me to make the most sense. But then I'm not too familiar with all the code yet so that's why I ashed if there was a better way ;)
– anon48034950Added code above
– DaveAI tried that Dave, still loops the sound and cuts out my others sounds of the Zombie attacking?
– anon48034950