Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rock-in-rio · May 26, 2011 at 04:53 PM · collisionsoundaudiosource

Collide sound

I hope it is very simple.I have a an object and when it is thrown down to the floor i want a sound to be played(just for the collision,like"boop").So,i have the sound clip and i've made an audio source on the floor,in which i put the clip.How can i connect the collision with the sound ?I can't think of a script..

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · May 26, 2011 at 05:34 PM

You could use the OnCollisionEnter event to trigger the sound, like this:

 function OnCollisionEnter(coll: Collision){
   GetComponent<AudioSource>().Play();
 }

The audio source and this script should be assigned to the floor object (it works with the terrain too). Remember to uncheck Play On Awake in the audio source, or you will hear the sound every time the game starts. Set also the Doppler Level to 0, or the sound may become unrecognizable when you're moving fast. Finally: the object which will fall to the ground must have a rigidbody to trigger the event.

NOTE: This answer was updated to Unity 5 by replacing the audio "variable" with the code GetComponent<AudioSource>() (which previous versions of Unity called behind the scenes anyway).

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 rock-in-rio · May 26, 2011 at 06:02 PM 0
Share

Thank you very much.You solved my problem and gave me some extra info ! :D

avatar image Meltdown · May 26, 2011 at 07:36 PM 1
Share

Please mark aldonetto's answer as the correct answer for your question so other's know your question has been resolved.

avatar image bjornsyse · Jun 18, 2017 at 12:40 PM 0
Share

Any drawback on assigning this script to the throwed object ins$$anonymous$$d?

avatar image aldonaletto bjornsyse · Jun 20, 2017 at 01:37 AM 0
Share

No trouble, as long as the script and the AudioSource both are part of the same object. The only difference is: if attached to the moving object, the sound will move with it. Not a big deal for short sounds, but may make a difference for longer ones. NOTE: the 'audio' variable has been removed in more recent Unity 5 versions, thus I've replaced it with the more correct GetComponent() code (which older versions called behind the scenes anyway).

avatar image
0

Answer by DaveA · May 26, 2011 at 04:59 PM

This should give you some hints: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

http://unity3d.com/support/documentation/ScriptReference/AudioSource.Play.html

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider + multiple sounds script 1 Answer

Audio Loops? 1 Answer

When is audio file loaded to memory 0 Answers

Only able to play a single sound through AudioSource. 1 Answer

Unity3D Playing Two Sounds - OnCollision 1 Answer


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