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
1
Question by munaeem · Oct 09, 2012 at 11:40 AM · audiosound

Sound on collision not working

Hi, i am using this script:

 var crashSound : AudioClip; // set this to your sound in the inspector
 
 function OnCollisionEnter (collision : Collision) {
     // next line requires an AudioSource component on this gameobject
     audio.PlayOneShot(crashSound); 

}

OK, so what i did is when my Player (tagged as player) collides with this object which is a sphere is should create a sound. Sound does not get played i tried searching form many scripts but still no luck out here. I did put an audio source to my sphere and audio listener to the camera and also tries attaching this script to my player as-well but sound doesn't get played. This script was found in other question.

Comment
Add comment · Show 1
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 hvilela · Oct 09, 2012 at 11:48 AM 1
Share

The first thing you should do it to make sure that OnCollisionEnter is called. Please, add a Debug.Log().

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Oct 09, 2012 at 11:59 AM

If your player is a CharacterController (like the First Person Controller prefab, for instance), this won't work: collisions rigidbody->characterController generate OnCollision events in the rigidbody script, but collisions characterController->rigidbody don't.
Collisions follow some schizophrenic rules - specially when a character controller is colliding with something else. The usual collision event associated to character controllers is OnControllerColliderHit, but the event is generated only in the CC script - and it happens all the time due to collisions CC->ground.
If the only purpose is to generate a sound when the player hits the sphere, you could make the sphere collider a trigger (check Is Trigger in the Inspector) and use OnTriggerEnter:

 var crashSound : AudioClip; // set this to your sound in the inspector

 function OnTriggerEnter (other: Collider) {
   // next line requires an AudioSource component on this gameobject
   audio.PlayOneShot(crashSound); 
 }

The only problem here is that the CC could pass through the sphere, since triggers are "transparent" from the collision point of view. A simple solution would be to create another sphere a little smaller and place it at the same position: the CC would be detected by the bigger trigger, but could not pass through the smaller collider.
NOTE: The CC must penetrate the trigger deep enough to be detected, thus the trigger must be about 0.2 to 0.4 units larger than the internal collider.

Comment
Add comment · Show 2 · 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 munaeem · Oct 09, 2012 at 01:22 PM 0
Share

what is CC ?

avatar image Mander · Oct 09, 2012 at 01:33 PM 0
Share

Character Controller

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

11 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Stop another sound from another script? 1 Answer

How to play Audio Once if enemy has enterd a trigger 1 Answer

How to loop a sound clip on a ButtonDown function 1 Answer

Need help making an audio trigger. 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