• 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 Harry64 · Jul 31, 2013 at 08:32 PM · collisionrigidbodysoundparentchild

get the OnCollisionEnter or Stay from the parent?

Hi there!

I have here a little problem going on. I have a Cube with a BoxCollider and a Rigidbody on it.

I want to have sound effects from the Cube when it hits something with a specific force and when the Cube is sliding on the floor. So I wrote a script with OnCollisionEnter and OnCollisionStay and a AudioSource and put them into the Cube.

but now if the Cube hits the floor the OnCollisionEnter sound is canceled right after that because it will sometime slide on the floor a little bit and so the OnCollisionStay overwrites the sound that should to be played with the sliding sound.

so I came up with the Idea to make 2 AudioSources but on One Gameobject it wont work. so I put 2 emptys with AudioSources on the Cube and rewrote the script for each.

but now I dont get any respond from the scripts and thats because the scripts dont have any Rigidbodys or colliders...

so how do I get the collision info to the child emptys?

here is the impact script from the child object.

 #pragma strict
 @script RequireComponent(AudioSource)
 
 var impactMagnitude : float = 0.01;
 
 var crVelocity : Vector3;
 var crMagnitude : float;
 
 
 function OnCollisionEnter (collision : Collision)
 {
     // Debug-draw all contact points and normals
     for (var contact : ContactPoint in collision.contacts)
     {
         Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
 
     
     crVelocity = collision.relativeVelocity;
     crMagnitude = collision.relativeVelocity.magnitude;
     
     var propInfo = transform.parent.gameObject.GetComponent(Prop_Info);
     
     if (collision.relativeVelocity.magnitude > impactMagnitude)
     {
         audio.clip = propInfo.PropSoundFX.impact[Random.Range(0,propInfo.PropSoundFX.impact.length)];
         audio.volume = (collision.relativeVelocity.magnitude / 5 + 0.5);
         audio.pitch = (Random.value * 0.4 + 0.8);
         audio.Play();
     }
 }
Comment
Add comment · Show 2
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 sdgd · Jul 31, 2013 at 09:04 PM 0
Share

don't know nothing about audio but did you try making 2 audio outputs?

like let say if you make only 1 int variable you overwrite it when you add it a new value.

so making 2 int-s makes you ability to have 2 values, ...

so having 2 audio outputs somehow.

OR if that's not possible as I looked in to your code what about making a child empty game object with audio component and using that audio for floor, ...

but that drives me insane for character to be able to have 30 sounds at once possible that would have to have 30 chldren for it, ...

avatar image Harry64 · Jul 31, 2013 at 11:19 PM 0
Share

yes that is that what bothers me. If I would need to play almost 30 sounds from 1 object at the same time. (theoretically) then I would need 30 children with audiosources.... it would be easier if I could give the audiosources names in the editor like audiosource 001 audiosource 002 and so on

then I would be able to have more audiosources from 1 Gameobject and I could access it via script and all that in one Gameobject.

but thank good no one on earth would do such a horrible thing... 30 sounds at the same time lol.... ^^

please read my answer down there.

2 Replies

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

Answer by danilonishimura · Jul 31, 2013 at 09:18 PM

The sound is canceled because you're using the same audio source for both sounds. Perhaps you should ignore the audio property of the GameObject and use two AudioSource objects and play the hit sound in one and the slide sound in other.

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 Harry64 · Jul 31, 2013 at 11:10 PM

sooo.... what you 2 say is I should not use the OnCollisionEnter or Stay function in the empty Child objects because the children dont have any collider and its not possible to send the Collision info from the parent to the children??

and instead I should write the script for the Cube and I should just make 2 var : Audiosource in the script and tell the script on awake it should look in the children for the audiosources? I think that sounds ok.

but really, is it not possible to send the

 function OnCollisionEnter(collision : Collision)

information from the collsion that happens to the children?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Make a simple tree 1 Answer

Rigidbody constraints not working with parent/child relationship 2 Answers

Player object still gets destroyed even when shields up 1 Answer

i cant get an object to become child of an other JS 1 Answer

OnCollisionEnter(Collision Other) setActive affecting parent 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