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 rd42 · May 04, 2010 at 07:25 PM · collisionaudioiphone

Sound on collision with rigidbody and rigidbody

I'm attempting to play a sound when two rigidbody collide, a ball hitting a cube. Do I attach the sound script to the ball? Do I attach the sound file to the ball too? and this is my code. I copied it from the forums and can't seem to get it working. Thanks for your help.

var body = other.rigidbody; var thump: AudioClip;

function OnCollisionEnter(collision : Collision) {

audio.Play(thump);

}

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
3

Answer by qJake · May 04, 2010 at 07:41 PM

That script is really incomplete. "audio" isn't defined anywhere, for starters.

If you want something to play on collision, attach an AudioSource to the object you want to make the sound. Load a sound clip into the audio source (all with the Inspector, no code yet). Then, attach something like this script to the same Game Object that you have your Audio Source attached to:

// C# public class CollisionAudioPlayer : MonoBehaviour { AudioSource asrc;

  void Start()
  {
       asrc = GetComponent(typeof(AudioSource)) as AudioSource;
  }

  void OnCollisionEnter(Collision other)
  {
       if(asrc != null)
       {
            asrc.Play();
       }
  }

}

Comment
Add comment · Show 5 · 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 StephanK · May 05, 2010 at 05:56 AM 0
Share

"audio" is a member of $$anonymous$$onoBehaviour and is a reference to this GameObjects AudioSource or "null" if no AudioSource is attached to this GO.

avatar image qJake · May 05, 2010 at 07:07 AM 0
Share

Thanks, updated my code. It's hard to remember what all the properties are without IntelliSense helping me. ;)

avatar image rd42 · May 05, 2010 at 01:32 PM 0
Share

Thanks again for your help SpikeX and you to spree. I have a newb question. The actual sound file I want to play, call it "impact". I drag that onto the game object and do I mention it's name anywhere in this script? Like AUdioSource: impact; ? I'm also getting error CS8025: Parsing Error on line 6, which is the bracket under void Start for me.

avatar image chearner · May 05, 2010 at 02:28 PM 0
Share

What happens if you have two balls that collide to each other (instances). Each clone would have the same audio file and same script so do they play at the exact same time over each other? or would the audio files cut each other out or cause weird sounds?

avatar image qJake · May 05, 2010 at 10:43 PM 0
Share

Fixed my syntax error... somehow my parenthesis magically disappeared on me. You reference the sound file in your AudioSource component... go look on the Unity website under "Audio Components", all of this is explained in detail there.

avatar image
1

Answer by rd42 · May 05, 2010 at 01:37 PM

Here's what I did and it worked. I attached the sound to the GO like you suggested, then out of the OnCollisionEnter documentation I used this,

function OnCollisionEnter(collision : Collision) {
      // Play a sound based on size of impact.
      if (collision.relativeVelocity.magnitude > 0)
      audio.Play();
   }
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 rd42 · May 05, 2010 at 01:38 PM 0
Share

What I'm not sure of now is how to attach and reference multiple sounds to one game objects...

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

IPhone 4 and IPad 2 lost audio on resume 0 Answers

iPhone pitch not working? 1 Answer

Change Pitch on Collision 1 Answer

How to NOT play audio in silent mode with iOS? 1 Answer

How to avoid 'auto audio volume decrease' on iphone? 3 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