Dynamic ball rolling sound

Hey everyone,

So I have a short sound effect of a ball rolling, but I am having a hard time getting it to sound right in the game. Basically, I want it to change based on how fast the ball is rolling, volume and pitch I guess. I am not to sure what I should be doing with it in code to get it to sound right. Any help would be appreciated!

If you are using RigidBody, you can get the velocity or angular velocity

From velocity can take magnitude,
and probably clamp the value, so you have minimum-maximum values that are still good for volume and pitch.

*links go to script docs

I was able to get something working. It does still need some tweaking, but that was actually a huge help, thanks!

Are you able to show me how exactly this would be written? (In c#) thanks!

Try something first, and copy your code here so we can help to modify it.

So this is what i have so far, my pitch on the rolling sound is way to high, and speeds up too fast, and the sound plays even when im not contacting the ground, somewhat lost with this dynamic sound movement.

public class RollingSound : MonoBehaviour
{
public Rigidbody rb;
public AudioClip audioClip;
public AudioSource audioSource;
public int startingPitch = 2;
public int volumeSpeed;

void Start()
{
rb = GetComponent();
audioSource.time = volumeSpeed;
audioSource.pitch = startingPitch;
}

void FixedUpdate()
{
audioSource.volume = Mathf.Clamp01(rb.velocity.magnitude / volumeSpeed);
audioSource.pitch = Mathf.Clamp(rb.velocity.magnitude / startingPitch, 1, 2);

if (rb.angularVelocity.magnitude >= 1)
audioSource.Play();
else
audioSource.Stop();
}
}

would probably have to set some maxspeed variables to be able to scale the value properly.

i tested something like this, it uses animationcurves,
so you can easily play with the curves to get suitable sounds based on the 0-1 speed value.

using UnityEngine;

public class Soundrb : MonoBehaviour
{
    public Rigidbody rb;
    public AudioSource audioSource;

    public float maxSpeed = 3.7f;
    public AnimationCurve volumeCurve;
    public AnimationCurve pitchCurve;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        var speed = rb.velocity.magnitude;
        Debug.Log("speed= " + speed);

        // normalize speed into 0-1
        var scaledVelocity = Remap(Mathf.Clamp(speed, 0, maxSpeed), 0, maxSpeed, 0, 1);

        // set volume based on volume curve
        audioSource.volume = volumeCurve.Evaluate(scaledVelocity);

        // set pitch based on pitch curve
        audioSource.pitch = pitchCurve.Evaluate(scaledVelocity);
    }


    // https://discussions.unity.com/t/465623
    public float Remap(float value, float from1, float to1, float from2, float to2)
    {
        return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    }

}

hmm this doesnt seem to be working for me

hmm this doesnt seem to be working for me :confused:

is there error, or no sound?
also needs to set some curve in inspector for those 2 curve fields. (its empty initially)

i set the curve and everything, the sound plys for a second then clicks in and out very fast and just isnt working.

forget to tag the reply

have you set some maximum speed, currently the script prints speed value, so you can play your game
and see roughly what is the maximum value, then assign that value in inspector.