- Home /
Changing Audio Pitch Dynamically
Hi guys,
I want to have an object that can be manipulated to change the pitch of a sound that is playing as it plays, and stay that way until the object is Transformed again.
I really just need to know if I'm generally on the right track with my logic here, and if my planned scripts and objects they will be attached to is correct, or if I need to learn other principles. I'd like to avoid getting into bad habits early on!
I'm still learning the different sorts of variables and functions in the scripting etc, but what I was thinking:
I think I'd need three types of objects with their own script each, to achieve what I want...? 1. When clicked, plays specified sound at specified pitch 2. Selects the sound 3. Sets the pitch
Really rough pseudo-scripting (if it can even be called that!) below:
Object 1. When clicked, plays sound at pitch
play{
the sound declared by Object 2 at the pitch declared in Object 3
}
I'm guessing this object would also need an Update function to pull the variables from the other objects on every frame so it always knows what the pitch should be?
Object 2. When clicked, selects which sound to play I'm not sure on the types of variables to use here..
var object var sound
if gameObject(1){ the sound gets changed to 1 else if gameObject(2) the sound gets changed to 2 etc, etc, etc }
Object 3. When clicked and dragged, changes the pitch of the sound being played
var pitch float = 1.0
When the user clicks and drags the object {
Transform.Rotate around single axis and set the "pitch" variable to a
number relative to its original rotation. So, as it Rotates one way,
it would increment, the other way, it would decrease, and store variable
}
Thanks guys, I'd really appreciate your feedback/advice.
(Edit: I just realised a simplification of what I want to do. You know how on some keyboards (musical) you have that little pitch bend wheel? My Object2 would be one of many keys, Object3 would be the key on the keyboard choosing your note, and imagine the sound wont play until Object1 is clicked, rather than when Object2 is.)
That is a long question I'd shorten it a bit to get sharp answers.
Hmm... I thought I'd get less answers if I was too vague. I don't want to be one of those "I wanna do this! Give me code to Copy/Paste!" guys.
Answer by Nicolaj Schweitz · Apr 13, 2010 at 08:37 AM
Start by reading this overview of how audio works in Unity.
Then, go check the docs on audio.pitch.
Basically you can use any floating point to set the pitch of an audio source, so I guess you could take a specific axis of your Object 3, and convert that axis' angle to a reasonable float that can be used with audio.pitch. E.g. using Quaternion.eulerAngles.y
Another thing you would want to take a look at first, is how you can drag the rotation of Object 3. First have the mouse's screen position converted to a 3d world coordinate (e.g. Camera.ScreenPointToRay
Then set the object's rotation based on the mouse movement while the mouse button is down. You can fin several handy scripts for this on the UnifyCommunity wiki's script sections.
Good luck.
Cheers! I'm away from my machine for a few days but I'll try as soon as I can. Thanks for the help! :-)