• 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 Irsan · Apr 26, 2011 at 07:48 AM · collisionrigidbodyvelocitysoundvolume

Sound volume based on rigidbody velocity before collision.

I found this, but as to using that in an if-statement, I have no idea. Here's me poorly attempting to come up with a snippet of code that might be a part of what I need to do:

var Hitsound : AudioClip;

function OnCollision (collision : Collision) { if (collision.relativeVelocity.magnitude > 0.5) { ??? } }

What I'm basically trying to do is to have the sound volume directly match the velocity of the rigidbody before it collides, so it sounds more natural. I could really use some help with this.

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 Fattie · Dec 14, 2012 at 08:58 AM 0
Share

well you need to know the RANGE OF COLLISION SPEEDS of your object.

(FTR if you wanted to be a smartrse, use momentum rather than velocity.)

you must use meters and kilograms in Unity, of course.

if you can't instantly convert in your head between m/s and km/h and mph, you'll struggle / never be able to resolve this issue, so get that sorted out.

So now, you tell me what's the typical $$anonymous$$AXI$$anonymous$$U$$anonymous$$ speed at the time of the collision?

(obviously, if it's say two racecars colliding, that might be 200mph. if it's two joggers colliding, it might be 15 mph, say)

So you'll tell me siome maximum figure in meters/second .. call that CV$$anonymous$$AX

so then it's just the collision speed divided by that number. And clamp the result between (say) 0.2 and 1.0.

so that's the volume to apply.

finally add a Debug.Log statement that prints out the relativeVelocity.magnitude each tie you crash. watch these as you play and learn the typical speeds.

hope it helps

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Baalhug · Apr 29, 2019 at 12:17 AM

This is very old. I'm trying to solve this problem too, but anyway the code above has an initial problem: volume must be a value between 0 and 1, where 0 is 0% of the volume of the clip and 1 is 100%. So if you do audio.volume = collision.relativeVelocity.magnitude; this will fail because the velocity magnitude can be greater than 0. You can use Mathf.Clamp01 but if most of the times velocity on collisions is higher than 1, all those collisions will have volume = 1 so you will notice no difference in a collision with velocity = 2 and velocity = 20. If velocity on impact is random (the object can vary a lot its own velocity) you must get a max value (aprox) and divide by it. For example if most of hits are in the range of 5 - 20 velocity, you can do:

 audio.volume = Mathf.Clamp01(collision.relativeVelocity.magnitude / 20);
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 muzboz · Mar 05, 2020 at 10:39 PM 0
Share

@Baalhug Exactly what I needed! Thanks. :)

avatar image doodler345 · Aug 06 at 09:23 AM 0
Share

That helped. Thanks!

avatar image
0

Answer by HammockHead · Dec 14, 2012 at 08:51 AM

off the top of my head something like this...it doesnt work but it might help put you in the right direction (hopefully)

EDIT: Fixed ambiguous reference, still will crash unity though

 //unstable, will crash Unity
 
 @script RequireComponent (AudioSource)
 
 var collide : AudioClip[]; //array of audio clips
 
 var audioVolume : float; //volume
 
 var audioLength : float = 1; //length
 
 function OnCollisionEnter(collision : Collision) { //on collision
 
 audio.clip = collide[Random.Range(0, audioLength )]; //random audio clip from array
         
 audio.volume = collision.relativeVelocity.magnitude; //set volume to speed of object
 
 audio.Play(); //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
0

Answer by RayznGame · Feb 06 at 01:10 PM

This works ideal for your case

 hitsound.volume= rb.velocity.sqrMagnitude / 100;

Tweak the values to your liking,

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get collision force, not relative velocity? 1 Answer

Sound volume and pitch based on speed of rigidbody and/or normal object? 1 Answer

get the OnCollisionEnter or Stay from the parent? 2 Answers

Rigidbody Wall Collision Bug/Wall Jitter 0 Answers

Jumping from a conveyor belt with added velocity 0 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