- Home /
someone is there?
Hi! I have a question to apply a "stop" to this script .. to tighten ("shiftUp" button) I want the audio stops for a while (in this case the value of ShiftTime)
Here I give you the script in c # : using UnityEngine; using System.Collections;
public class prueba : MonoBehaviour {
public AudioClip test;
public float testvolumenMax = 1;
public float testpitch = 1;
public float ShiftTime = 0.8f;
AudioSource testSource;
Drivetrain drivetrain;
float volumeFactor;
int i,k;
AudioSource CreateAudioSource(AudioClip clip, bool loop, bool playImmediately, Vector3 position) {
GameObject go = new GameObject("audio");
go.transform.parent = transform;
go.transform.localPosition = position;
go.transform.localRotation = Quaternion.identity;
go.AddComponent(typeof(AudioSource));
go.audio.clip = clip;
go.audio.playOnAwake = false;
if (loop==true){
go.audio.volume = 0;
go.audio.loop = true;
}
else
go.audio.loop = false;
if (playImmediately) go.audio.Play();
return go.audio;
}
void Start () {
drivetrain=GetComponent<Drivetrain>();
testSource = CreateAudioSource(test, true, true,Vector3.zero);
}
void Update () {
if(Input.GetButton ("Throttle")){
if(this.drivetrain.rpm>100){
{if (!testSource.isPlaying) testSource.Play();}
float factor=drivetrain.rpm/drivetrain.maxRPM;
testSource.volume = 0 + testvolumenMax*factor;
testSource.pitch = 0.1f + testpitch*factor +0.3f;
}
}
else
{
float factor=drivetrain.rpm/drivetrain.maxRPM;
testSource.volume = 0.1f + testvolumenMax*factor-0.1f;
testSource.pitch = 0.1f + testpitch*factor -0.15f;
}
}
}
how I can apply a stop time to audio? help me? Thanks!
ShiftTime is not defined in your script.
In order to stop the audio, you need to keep a record of all the 'go' objects you are creating - a them to a List. Then set a function to execute after 'ShiftTime' time, after the creation. When the new function executes, remove the first item from the List.
Hope this helps.
Answer by richyrich · Nov 13, 2014 at 03:59 PM
Add another function:
IEnumerator StopAudio(AudioSource asToStop)
{
yield return new WaitForSeconds(ShiftTime);
asToStop.Stop();
}
In Start, underneath testSource = CreateAudioSource(test, true, true,Vector3.zero); add:
StartCoroutine(StopAudio(testSource ));
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
play audio for few seconds on key hit 1 Answer
Vehicle help 1 Answer
Gun Audio reverbs off of Terrian- sounds horrible- What can I do? 0 Answers
Wait for sound is played 2 Answers