Ball Rolling soundfx

Hi there,

I’m controlling a rolling ball (like in the Marble tutorial) and I want to apply a rolling sound effect. My script so fr is:

function Update ()
{

if ( Input.GetKeyDown (“up”) || Input.GetKeyDown (“down”) || Input.GetKeyDown (“left”) || Input.GetKeyDown (“right”))

{
** if (!audio.isPlaying)**
audio.Play();
}

if ( Input.GetKeyUp (“up”) || Input.GetKeyUp (“down”) || Input.GetKeyUp (“left”) || Input.GetKeyUp (“right”))

if (audio.isPlaying)
** audio.Stop();**

}

it works, BUT in order to hear the clip again I have to release one button and then press another button. When I’m pressing the control buttons (arrows) quicly I hear the clip for milliseconds. I don’t understand what’s going on. Any help???

You should just set the sound volume depending on the rolling speed.

var topSpeed = 0.00;

function Update ()
{
    audio.volume = Mathf.Clamp01(rigidbody.velocity.magnitude / topSpeed);
}

Try experimenting with things added into the equation and maybe apply it to pitch too.

It works just Great!!! thanx!

However, if you don’t mind, I would like to explain me what clamp01 does, cause I checked the script reference and I didn’t understand it quite well.

Also, the magnitude variable is reffered to the range of velocity (at least for this example!)???

It seems good idea to apply that to the pitch as well, thanx!!! :slight_smile: :slight_smile:

Mathf.Clamp clamps a value between two other values. The first argument is the value to clamp, then comes the min and max.

Mathf.Clamp(0, 2, 5) == 2
Mathf.Clamp(3, 2, 5) == 3
Mathf.Clamp(10, 2, 5) == 5

Mathf.Clamp01 simply does the same thing but automatically clamps between 0 and 1. It is a lot faster to type is all.

rigidbody.velocity is a Vector3. all Vector3s have a magnitude variable, which is the square root of all the x, y and z components squared and then added together.

So if you had a car the speedometer would read car.rigidbody.velocity.magnitude in meters per second.

Thanx a lot!!!

I 've noticed that the script works almost good without the “Mathf.Clamp01”, sooo if I got it right the clamp01 gives a value to the volume between 0 and 1. Right, huh??

I think I’m right cause I applied a similar think to the pitch of the clip

audio.pitch = Mathf.Clamp(rigidbody.velocity.magnitude / pitSpeed, 0.7, 1);

It just seems to work better if the value is “clamped” between 0.7 and 1!!!

thank you very much. I’m really new with JavaScript but with this helpful and supportive forum (and if you have the ideas and know what you want to do), you can improve your skills and knowledge. I feel much more confident now!! Hope I will improve more during the next months. Thanx!

Well, you can do whatever you want thanks to Unity :wink:

I’m still using this script to control the rolling soundfx of the ball

audio.volume = Mathf.Clamp01(rigidbody.velocity.magnitude / volSpeed);
audio.pitch = Mathf.Clamp(rigidbody.velocity.magnitude / pitSpeed, 0.8, 0.9);

…but… very often I can hear random tiny “clicks” when the volume and the pitch is manipulated very fast…

…any ideas??

edit: When the clamp of the pitch is between any value and 1 the clicks are increasing

Is the audio sample you are using in .MP3 or .OGG? If so, that might be what is causing the clicking. Try a non-streaming format (like .aif) to see if it goes away.

Also, you might try to change the KhZ…sometimes clicking dissapears when saving in lower KhZ…very odd because it doesn’t make sense.