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
4
Question by Sam Beckham · Apr 14, 2010 at 10:24 AM · javascriptcollisioncollideraudioaudioplay

Sound on collision

Hello all.

I have a simple script that makes a locker make 2 different sounds depending on what it collides with. If it collides with concrete it makes one sound and if it collides with another locker it makes a different sound.

Here it is:

var FloorSound : AudioClip; var LockerSound : AudioClip;

function OnCollisionEnter (other : Collision) { if (other.gameObject.tag == "Concrete") { audio.clip = FloorSound; audio.Play(); } else if (other.gameObject.tag == "Locker") { audio.clip = LockerSound; audio.Play(); } }

The problem I am getting is that at the start of the game the locker collides with the floor instantly and the sound goes off. Then because the locker never actually leaves the floor it doesn't make the sound again when I push it over. It collides with the other locker quite nicely so I know there isn't a problem as such with the code, it just needs something adding to it to keep it updating every time the locker collides with the floor.

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
3
Best Answer

Answer by duck · Apr 14, 2010 at 02:00 PM

You could probably fix this by examining the relativeVelocity reported by the collision in the OnCollisionStay method, and playing a sound if it exceeds some threshold. You could even change the volume of the sound based on the relativeVelocity.

Something like this:
(for illustrative purposes! - untested, may contain errors!)

var FloorSound : AudioClip; var LockerSound : AudioClip; var impactSoundThreshold = 0.5; // tweak this value

// both the "Enter" and the "Stay" collisions pass // their data to the sound function:

function OnCollisionStay (collision : Collision) { PlayCollisionSound(collision); }

function OnCollisionEnter (collision : Collision) { PlayCollisionSound(collision); }

function PlayCollisionSound (collision : Collision) {

 // check if the collision was hard enough to generate a sound
 if (collision.relativeVelocity > impactSoundThreshold) {

     // select the correct sound effect based on the object's tag
     switch (collision.gameObject.tag) {
         case "Concrete": audio.clip = FloorSound; break;
         case "Locker": audio.clip = LockerSound; break;
     }

     // calculate the volume (anything above 4*Threshold = full volume)
     var volume = Mathf.InverseLerp(impactSoundThreshold, impactSoundThreshold*4, collision.relativeVelocity);

     // play the audio
     audio.volume = volume;
     audio.Play();
 }

}

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
1

Answer by Sam Beckham · Apr 14, 2010 at 02:54 PM

Figured it out now. It's very similar to what Duck sugested.

var LockerSound : AudioClip;

function OnCollisionStay (collision : Collision) { if (collision.relativeVelocity.magnitude > 0.5) { audio.clip = LockerSound; audio.Play(); audio.volume = (collision.relativeVelocity.magnitude / 5 + 0.5); audio.pitch = (Random.value * 0.5 + 0.5); } }

I decided to take out the difference in sound files as this was complicating things a little and made some funny sounds but you can add it back in quite easily.

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 alexnode · Apr 14, 2010 at 11:02 AM

I am not a coder but I had something similiar ... If your locker is always on the concrete then you shouldn't use the collision with the concrete to activate the sound. Try to link it when you character is pulling it iPushLocker = true;. you should also consider looping the sound while you push...

var iPushLocker : boolean = false;

function OnCollisionEnter (other : Collision) {

if((other.gameObject.tag == "Locker") && (iPushLocker == true )) { audio.clip = FloorSound; audio.Play();

 }


}

and then add onCollisionExit to set the IPushLocker as false. Hope it helps a bit.

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

No one has followed this question yet.

Related Questions

Change Pitch on Collision 1 Answer

Getting audio to play on collision 3 Answers

OnCollisionEnter Collision not detected? 1 Answer

Audio on collision is not playing 1 Answer

Cannot detection collision 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