- Home /
Duplicate Question
Fade Audio
Hi guys, How can I do for make a fade at "Motore2" when i press A?
Sorry for my bad english!
using UnityEngine;
using System.Collections;
public class Audio : MonoBehaviour
{
public AudioClip Motore1;
public AudioClip Motore2;
float soundPitch = 0;
float maxSpeed = 2;
float minSpeed = 0.2f;
void Start()
{
if (!audio.playOnAwake) audio.Play();
}
// Update is called once per frame
void Update ()
{
soundPitch = Mathf.Clamp(soundPitch, minSpeed, maxSpeed);
if (Input.GetKey(KeyCode.A))
{
audio.clip = Motore1;
soundPitch+= 0.09f;
audio.pitch = (soundPitch);
}
if (Input.GetKey (KeyCode.D))
{
audio.clip = Motore2;
soundPitch+= 0.09f;
audio.pitch = (soundPitch);
}
if (Input.GetKey (KeyCode.S))
{
soundPitch-= 0.09f;
audio.pitch = (soundPitch);
}
}
}
You probably want to handle setting the clips separately, as in that code, it will reset the audio clip every frame if the key is held down.
Changing the pitch doesn't really seem like a fade to me.
This is a duplicate question. http://answers.unity3d.com/questions/639589/reduce-audio-volume.html
http://answers.unity3d.com/questions/640426/reduce-audioclip-when-a-key-is-pressed.html
I given you the starter code to handle what you are looking for along with the pseudo code you created. If you look at the code I gave you closely you will see that all you need to do is duplicate the case I gave you and change a few plus to $$anonymous$$us signs, then change the name of the vars for motor2.
I'm not sure what else to do to help other than write all of it out for you.
HAHAHA! @Andy$$anonymous$$artin458 - good point! $$anonymous$$ight wanna be adjusting the volume ins$$anonymous$$d. LOL.
@POLYGA$$anonymous$$e Indeed! I also agree with you about setting the clip each update frame. Not the right approach.
Answer by unimechanic · Feb 22, 2014 at 12:09 AM
Duplicate Question:
http://answers.unity3d.com/questions/639589/reduce-audio-volume.html
http://answers.unity3d.com/questions/640426/reduce-audioclip-when-a-key-is-pressed.html
[Adding this answer to remove it from the Unanswered list.]