- Home /
Theme music for approach to object question
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?
Answer by DaveA · Apr 07, 2011 at 08:27 PM
Set a boolean that indicates that it is playing and avoid calling PlayOneShot again, or check the .IsPlaying member on the audio source, and don't restart it if it's true http://unity3d.com/support/documentation/ScriptReference/AudioSource-isPlaying.html
if (!audio.IsPlaying && Vector3.Distance(transform.position, player.gameObject.transform.position) < playAudioDist)
{
audio.PlayOneShot(spookyChickenHouse);
}
I'd also recommend not constantly checking Vector3.Distance, since it's fairly expensive to use in Update. $$anonymous$$ichael, is there a reason you aren't using a collider/trigger-based system?
I 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 ;)
I tried that Dave, still loops the sound and cuts out my others sounds of the Zombie attacking?
Your answer
Follow this Question
Related Questions
Background Music 2 Answers
Background music 0 Answers
How do i add/import sound effects and/or music into my game? 5 Answers
How to mute music independent from other sounds using mixers? 0 Answers