Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michael 12 · Apr 07, 2011 at 08:17 PM · audiosoundmusic

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
  }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burnumd · Apr 07, 2011 at 08:34 PM 1
Share

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?

avatar image Michael 12 · Apr 07, 2011 at 09:03 PM 0
Share

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 ;)

avatar image DaveA · Apr 07, 2011 at 09:35 PM 0
Share

Added code above

avatar image Michael 12 · Apr 07, 2011 at 10:01 PM 0
Share

I tried that Dave, still loops the sound and cuts out my others sounds of the Zombie attacking?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

3D Audio: Selectively Apply the Doppler Effect. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges