i have this code that controls the thrusters of my ship and when i press space bar it adds force starts the emmitters and plays a sound when i let go spacebar everything stop but not the sound any sugesstions.
function Update () {
if(Input.GetButton("Jump")){
totalForce = totalForce + mainThrust;
pE = gameObject.Find("VectorBlast1").GetComponent(ParticleEmitter);
pE.emit = true;
pE = gameObject.Find("VectorBlast2").GetComponent(ParticleEmitter);
pE.emit = true;
pE = gameObject.Find("VectorBlast3").GetComponent(ParticleEmitter);
pE.emit = true;
pE = gameObject.Find("VectorBlast4").GetComponent(ParticleEmitter);
pE.emit = true;
rigidbody.AddForce(mainThrust * transform.forward);
audio.PlayClipAtPoint(Thrust, transform.position);
}
else
{
pE = gameObject.Find("VectorBlast1").GetComponent(ParticleEmitter);
pE.emit = false;
pE = gameObject.Find("VectorBlast2").GetComponent(ParticleEmitter);
pE.emit = false;
pE = gameObject.Find("VectorBlast3").GetComponent(ParticleEmitter);
pE.emit = false;
pE = gameObject.Find("VectorBlast4").GetComponent(ParticleEmitter);
pE.emit = false;
}
transform.eulerAngles = Vector3(0, 0, 0);
}
The sound doesn’t stop because you’re not doing anything that would make the sound stop.
Note that PlayClipAtPoint() probably isn’t what you want to use here, as it will create a new (temporary) game object that isn’t easily accessible. Instead, you’ll most likely want to play the sound using an AudioSource component that you have continuous access to so that you can call Stop() on that component when the thrusters are deactivated.
Note also that currently you’re starting playback of a new instance of the thruster sound on every update that the ‘thruster’ control is down (if it seems like it’s working, it’s probably just that you’re getting an overlapped, phased effect that has the appearance of being correct). What you’ll likely want to do instead is start and stop the sound in response to ‘button down’ and ‘button up’ events.
I believe that you need to set a update lerp system for the sound. So lets take you down a slightly different path…
Say your sound is looping, and always on but at zero volume. You press the space key and slowly(over half a second) the sound volume picks up to full volume. You let off, and over half a second it drops back to zero. (you can do this with the emitter too)
Now your code would look something like this: (I dont have sound working on my current pc at work, so it may need some testing)
var Thrust: AudioClip;
var TimeForSoundTransit=0.5;
private var NewSoundLevel=0.0;
private var TimeToNextSound=0.0;
private var CurrentSoundLevel=0.0;
function Start(){
audio.PlayClipAtPoint(Thrust, transform.position);
audio.volume=0.0;
audio.loop=true;
}
function Update(){
if(Input.GetButton("Jump")){
CurrentSoundLevel=audio.volume;
NewSoundLevel=1.0;
TimeToNextSound=Time.time + TimeForSoundTransit;
}else{
CurrentSoundLevel=audio.volume;
NewSoundLevel=0.0;
}
if(Time.time<TimeToNextSound){
audio.volume = Mathf.Lerp(CurrentSoundLevel, NewSoundLevel, (TimeToNextSound-Time.time)/TimeForSoundTransit);
}else{
CurrentSoundLevel=NewSoundLevel;
}
}
BigMisterB, I don’t believe your code will work as is; PlayClipAtPoint() creates a new audio source component to play the sound, so adjusting audio.volume will probably not have the intended effect. (Also, if the sound is played back via PlayClipAtPoint(), it will not be looped.)
thank you will look at this