Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change pitch on, depending on the speed of collision

Discussion in 'Scripting' started by okarad, Jul 25, 2022.

  1. okarad

    okarad

    Joined:
    Sep 14, 2018
    Posts:
    5
    I have a script that handles picking up props, dropping them, and having a collision sound, all of it works
    But i want to change how the collision sound works and i cannot figure out how to do it.

    What i want to do is in the title, change the pitch, depending on the speed of the collision.
    If the speed at which it collides with something is a fast speed, set to a high pitch and play.
    If the speed is low, set the pitch to low and play.

    Current script just randomizes the pitch on specific magnitudes.

    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.relativeVelocity.magnitude >= 3.6f)
    4.         {
    5.             colSound.pitch = (Random.Range(0.3f, 0.7f));
    6.             colSound.Play();
    7.         }
    8.  
    9.         if ((collision.relativeVelocity.magnitude < 3.6f) && (collision.relativeVelocity.magnitude > 1.70f))
    10.         {
    11.             colSound.pitch = (Random.Range(1.0f, 1.6f));
    12.             colSound.Play();
    13.         }
    14.     }
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,505
    Could do like
    colSound.pitch = Mathf.Clamp( 1 + (collision.relativeVelocity.magnitude * 0.01f), 1, 3 );
    , where the 0.01f scales how much of an effect the velocity has, and the 1 and 3 being the minimum and maximum you want to allow.
     
  3. okarad

    okarad

    Joined:
    Sep 14, 2018
    Posts:
    5
    This plays the sound on every collision but doesn't change the pitch at all, it's always 1.0.
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,505
    A few things come to mind:
    - Is collision.relativeVelocity.magnitude fast enough that 0.01f is a large enough multiplier? If your velocities are very low, you might change the 0.01f to like 1.0f and see if that makes enough of a difference.
    - It's possible somehow the clamp is being interpreted as/rounded to an int, maybe try changing the 1s and 3 to be 1f and 3f.
    - Have you removed all the old code and just have 2 lines in the function now, the new colSound.pitch = and the colSound.Play ?
    - Not necessarily helpful for this in particular, but worth noting that Debug.Log doesn't show many decimal places of Vector3 values by default, so if you want to log velocities or other stuff with more precision for debugging, you can do that using ToString("F5") like below for example, where the 5 means 5 decimal places:
    Code (CSharp):
    1. Vector3 example = new Vector3(0.001f, 0.0023f, 0.00456f);
    2. Debug.Log(example);
    3. Debug.Log(example.ToString("F5"));
    The result of the above is this - note it's all zeroes the first way:
    debug_log_rounding.png

    Not sure what else to suggest - you could look at what velocities you're working with and adjust the multiplier to suit that. Note that a multiplier of 0.01f would only make the pitch as high as 2 with a velocity magnitude of 100 (i.e. 1f + (100 * 0.01f)).
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    In Jetpack Kurt, I only manipulate volume for impacts... I tried pitch but it didn't really work because harder impacts start sounding like clicks. This is what I put on the Rigidbody:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker - part of Jetpack Kurt
    4. //
    5. // To add it to your Collider-equipped Rigidbody, call Attach();
    6.  
    7. public class BumpNoiseImpactSensor : MonoBehaviour
    8. {
    9.     // optional delegate to find out if you should be listening for bumps or not
    10.     System.Func<bool> GetActive;
    11.  
    12.     public static BumpNoiseImpactSensor Attach( Rigidbody rb, System.Func<bool> GetActive = null)
    13.     {
    14.         BumpNoiseImpactSensor impactor = rb.gameObject.AddComponent<BumpNoiseImpactSensor>();
    15.         impactor.GetActive = GetActive;
    16.         return impactor;
    17.     }
    18.  
    19.     void OnCollisionEnter( Collision collision)
    20.     {
    21.         if (GetActive != null)
    22.         {
    23.             if (!GetActive()) return;
    24.         }
    25.  
    26.         float HardestImpactSpeed = 0;
    27.  
    28.         foreach( ContactPoint cp in collision.contacts)
    29.         {
    30.             // computes how hard we slammed into the object
    31.             float velocityMagnitude = Mathf.Abs( Vector3.Dot (
    32.                 collision.relativeVelocity, cp.normal));
    33.  
    34.             if (velocityMagnitude > HardestImpactSpeed)
    35.             {
    36.                 HardestImpactSpeed = velocityMagnitude;
    37.             }
    38.         }
    39.  
    40.         if (HardestImpactSpeed > 0)
    41.         {
    42.             AUDIO.PlayBump (HardestImpactSpeed);
    43.         }
    44.     }
    45. }
    And here is the actual sound player:

    Code (csharp):
    1.     public static void PlayBump( float hardness)
    2.     {
    3.         hardness /= 20.0f;        // magic number :)
    4.         string s = "bump" + Random.Range (1, 4).ToString ();
    5.         Play (s, volSet: hardness);
    6.     }
    That just chooses from three (3) different bump noises for variety.



    The videos don't really capture the impact sounds well but you can try it yourself:

    Appstore: https://itunes.apple.com/us/app/jetpack-kurt/id1033348911
    GooglePlay: https://play.google.com/store/apps/details?id=com.plbm.jetpack
    Android TV: https://play.google.com/store/apps/details?id=com.plbm.jetpacktv

    Itch.io: https://kurtdekker.itch.io/jetpack

    Simmer.io: https://simmer.io/@kurtdekker/jetpack-kurt
     
  6. okarad

    okarad

    Joined:
    Sep 14, 2018
    Posts:
    5
    Sorry - Completely forgot about this post.
    I've tried Kurt's answer and Polemical's answer but got no results i'm satisfied with, but thanks for replying anyway.
    I figured a way to do it with two more if statements and I'm content with the results I got.
     
    Kurt-Dekker and adamgolden like this.