- Home /
Dynamic Sound: Wind up Wind down Audio
Id like to know if its even possible to make a script or anything of the sort that allows a button to be help and a sound winds up and when released the sound winds back down at the point in which you let it go. I know I could script with the OnMouseDown play this audio and OnMouseUp play this audio but Im trying to make it more dynamic but I have no idea where to start, how to set up the audio files, the script etc. Im working on a Siren Simulator that recreates real Siren Systems that Emergency Services use. Right now Im trying to create two sirens that both use this Dynamic Wind up and Wind down system but have no idea where to start with this idea and was hoping you guys could maybe hint me.
The 2 Sirens are the Federal Signal EQ2B and the FDNY Federal Signal PA300 which both have a manual siren option in which the Officer who rides in the Passenger's seat has a foot pedal on the right that winds the siren up and when released winds the siren down. On the left he has a foot pedal that runs the airhorn on the truck. Here is a video of the two sirens in action: (First truck has an EQ2B, Second has a PA300) http://www.youtube.com/watch?v=zuzmzv2AMQ8
FDNY PA300 Demo(I own this Siren): http://www.youtube.com/watch?v=e2bbxRr73AA
EQ2B Demo (I own this siren) : http://www.youtube.com/watch?v=U7KCAUvFzAU
Here is my progress on this one: http://www.youtube.com/watch?v=qJmzUZSzGkE
Answer by Sajidfarooq · Aug 16, 2013 at 07:18 PM
This should do pretty much everything you want:
using UnityEngine;
using System.Collections;
public class PitchChange : MonoBehaviour
{
public AudioSource Audio;
public float StartingPitch = 1.0f;
public float EndingPitch = 2.0f;
public int PitchScaleFactor = 3;
bool isMousePressed = false;
void OnMouseDown()
{
isMousePressed=true;
}
void OnMouseUp()
{
isMousePressed=false;
}
// Update is called once per frame
void Update ()
{
//If mouse is pressed, increase pitch
if(isMousePressed == true)
{
if(Audio.pitch <= StartingPitch)
{
Audio.Play();
}
//Only increase pitch if "EndingPitch" value has not been reached
if(Audio.pitch <= EndingPitch)
{
Audio.pitch += ((Time.deltaTime * StartingPitch) / PitchScaleFactor);
}
}
//Otherwise, decrease pitch back to normal, until we reach starting pitch
else
{
if(Audio.pitch > StartingPitch)
{
Audio.pitch -= ((Time.deltaTime * StartingPitch) / PitchScaleFactor);
}
else
{
Audio.Stop();
}
}
Debug.Log(Audio.pitch);
}
}
USAGE
Create a "beep" audio clip, and add it to the scene as an audio source.
Create a button and attach this script to it.
Drag the audio source onto the button. Click the button to "wind-up" and release to "wind-down" back to normal
Thanks this looks like it could work, Im having a script error :Assets/Siren.cs(1,20): error CS0116: A namespace can only contain types and namespace declarations on this script I have no idea why so I havent been able to try it out yet. Is there any way that I could change the time it takes to wind down slower than the time it takes to wind up? Any help with the script and that request would be really appreciated! Im in a ditch here.
I tested the code thoroughly before posting, and now again. It works. Are you declaring anything "before" the class name?
Anyway, I have edited the code so you can copy-paste. Create a new c# script by the name of "$$anonymous$$chChange.cs". $$anonymous$$ake it completely empty, and copy-paste the entire code above into it. It should work.
Thanks but im still getting this when I try to test the function: Assets/$$anonymous$$chChange.cs(40,1): error CS8025: Parsing error
the script is telling me that last bracket doesnt belong? Doesnt make any sense >.<
If possible could you send me the project your testing on?
(NV$$anonymous$$ GOT IT TO WOR$$anonymous$$, problem was there was a bracket that you didnt put in the spoiler at the end. Thanks very much!
However the only way Ive gotten it to work is by on the audio source selecting it to loop and play on awake, anyway to make it turn off at the end of the pitch down?
I see the problem. For some reason, Unity Answers didn't select my last bracket in the "Code" tag even though I selected it.
Anyway, it should work now. If not, give your email address and I will send you the file.
Edited code to include "start" and "stop" as requested.