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 Nightgecko · Feb 18, 2013 at 11:56 PM · collisionsoundfpc

Sound play on collision

In the game that I am making I have many collectables around the environment that you have to gather. I have scripts set up so when the item is collected, the gameObject is destroyed and a GUI counter is incremented.

On top of this I would like for a sound to play when you pick up a collectable. Since the object is destroyed I am not able to simply attach it to the game object for the sound does not play. So I looked on the forums and ended up doing PlayClipAtPoint.

This works but does not have the exact result that I want. The sound will play just fine, but since I am generally moving through the item and just keep going to find a new collectable, the 2-3ish seconds of sound just disappears/becomes warped by the Doppler effect.

Is there a way to have the audio "follow" the First Person Controller OR make the audio file play over the entire scene and have no falloff/Doppler effect?

Here is my currently working code:

 static var FEATHERS = 0;
 var pickupSound : AudioClip;
 
 function OnControllerColliderHit(hit : ControllerColliderHit)
 {
     if(hit.gameObject.tag == "featherPickup")
     {
         var feathersLEFT = 60 - FEATHERS;
                 
         //play sound effect
         AudioSource.PlayClipAtPoint(pickupSound, transform.position);
         
         //increment feathers
         FEATHERS ++;
         
         //print feathers remaining
         print("Feather aquired " + feathersLEFT + " feathers remaining!");
         GameObject.Find("GUI_FeatherCount").guiText.text = ""+FEATHERS;
         
         //make feather disapear
         Destroy(hit.gameObject);

         if(FEATHERS == 60)
         {
         Application.LoadLevel(3);
         print("All feathers gathered, go to the next level!");
         return;
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Professor Snake · Feb 19, 2013 at 12:12 AM

Mark your sound as 2D in the import settings and/or add an audio source to your player object and simply use:

 audio.PlayOneShot(pickupSound);

Marking your sound as 2D will make it play at all times regardless of distance and be unaffected by the doppler effect.

Comment
Add comment · 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
0

Answer by $$anonymous$$ · Feb 19, 2013 at 12:12 AM

One solution would be to not destroy the object at all, but just hide it disabling the renderer component. Just use a boolean to check if the feather is picked up or not, so you can still use an audiosource and have the sound play like you want.

But I f I get it right, that code is on your player, so instead of trying to play the sound from the feather itself, you can just add an audiosource component to your player as a child if you already have another one and play the sound there.

Also I would make a suggestion, why don't you use OnTriggerEnter instead of OnControllerColliderHit? It would be faster, but that is just me beign nitpicking.

Comment
Add comment · 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
0

Answer by Nightgecko · Feb 19, 2013 at 04:19 AM

I opted for the first answer of adding an audio source to the player object and using PlayOneShot. It works exactly as I want it to, thank you so very much.

As for the second answer, I made the script straight off of a tutorial pretty much and they just happened to use OnControllerColliderHit. And I am new to Unity scripting as well, so don't know anywhere close to all the tricks yet. Also, I just like to destroy the object to make the game more "efficient" by not having as many things in the game, even though my game is very small.

Again thank you both for your answers, I really appreciate it.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Feb 19, 2013 at 11:43 AM 0
Share

Just an heads up, to take Professor Snake answer as correct one, you have to click the V button under the down/up vote icons.

By the way I would like to give you small tip on that line:

if(hit.gameObject.tag == "featherPickup")

Would be faster to use CompareTag, so it would be like:

if(hit.gameObject.CompareTag("featherPickup"))

Of course the impact of this is very small, but it can be always usefull in more complex code.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

public variable audio clip not initializing sound 0 Answers

help sound on collision when key down 2 Answers

How can I play audio while a non player controlled object is moving and stops playing when object stands still? 1 Answer

Multiple/Different footsteps sounds (solved) 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