Reduce audio volume

I would like to reduce audio “Motore2” when i press A in void update… but there’s yet another audio.clip, so I cannot make audio.volume = 0.05f… etc…
How can I do?

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);
       }
 
    }
 
 
}

From the comments under the answer by JeffreyD :

if push on accelerator there is a sound, if the car go reversing there is a reversing suond. I would make the same suond effect, with motore1 and motore2. So, if click on ‘A’ increase motore1 untile its pitch limit (3.0f) but if push ‘D’ the suond of motore1 decrease and then stop, and increase motore2! is it clear, no?

Scheme: if input key A, motore1 start and increase until its pitch limit, if motore2 is playing decrese and stop it (motore1 continue to run)

if input key D, motore2 start and increase until its pitch limit, if motore1 is playing, decrese and stop it (motore 2 continue to run)

If input key S, motore1 and motore2 decrease and stop them.

My response :

You are not making this any easier to understand.

Engine sound is usually directly related to the revs of the engine. So I have written a script that uses key inputs to modify a revs variable, then made all pitch calculations based on the revs.

Note : Unity Answers is not for people to write you custom made scripts. I have chosen to do this so you can get started, and stop posting duplicate questions.


Now, my answer is in unityJavaScript, but this is just a test to make sure I have what you want correct. SO follow these steps to test :

  • create a new scene
  • create a new gameObject, name it SoundPitch
  • create a new gameObject, name it Motore1. Add Component AudioSource. Drag and drop your motore1 sound into the Audio Clip slot
  • create a new gameObject, name it Motore2. Add Component AudioSource. Drag and drop your motore2 sound into the Audio Clip slot
  • create a new javascript, name it SoundPitch, copy and paste the below code in. Attach the SoundPitch to the SoundPitch gameObject
  • drag and drop Motore1 AudioSource into SoundPitch Motore 1 slot
  • drag and drop Motore2 AudioSource into SoundPitch Motore 2 slot

see the image :

here is the script :

#pragma strict


public var motore1 : AudioSource;
public var motore2 : AudioSource;

public var engineRevs : float = 0.0;
public var engineSpeed : float = 0.09;
private var engineLimit : float = 1.0;

public var minSpeed : float = 0.2;
public var maxSpeed : float = 2.0;

public var pitch1 : float = 0.0;
public var pitch2 : float = 0.0;


function Update() 
{
	// forward button
	if ( Input.GetKey(KeyCode.A) )
	{
		engineRevs += engineSpeed * Time.deltaTime;
	}
	
	// backward button
	if ( Input.GetKey(KeyCode.D) )
	{
		engineRevs -= engineSpeed * Time.deltaTime;
	}
	
	// button to lower both pitches
	if ( Input.GetKey(KeyCode.S) )
	{
		// send engineRevs to zero
		// determine if stopping from forward or backwards
		if ( engineRevs > 0 ) // forward
		{
			engineRevs -= engineSpeed * Time.deltaTime;
		}
		else // backward
		{
			engineRevs += engineSpeed * Time.deltaTime;
		}
	}
	
	// clamp value
	engineRevs = Mathf.Clamp( engineRevs, -engineLimit, engineLimit );
	
	// --
	
	// set both pitches based on revs
	
	// going forward or backwards?
	if ( engineRevs > 0 ) // forward
	{
		pitch1 = Mathf.Clamp( engineRevs * maxSpeed, minSpeed, maxSpeed );
		pitch2 = minSpeed;
	}
	else // backward
	{
		pitch1 = minSpeed;
		pitch2 = Mathf.Clamp( -engineRevs * maxSpeed, minSpeed, maxSpeed );
	}
	
	// --
	
	// set pitches on both audioSources
	motore1.pitch = pitch1;
	motore2.pitch = pitch2;
}

.


If this is what you want, I shall convert it to C#

Edit : here is the above script in C#. I have also added the check to see if both audioSources are playing on Start. I also included setting both the audioSources to loop (as I assume you would want them to). :

using UnityEngine;
using System.Collections;

public class SoundPitch : MonoBehaviour 
{
	public AudioSource motore1;
	public AudioSource motore2;
	
	public float engineRevs = 0.0f;
	public float engineSpeed = 0.09f;
	private float engineLimit = 1.0f;
	
	public float minSpeed = 0.2f;
	public float maxSpeed = 2.0f;
	
	public float pitch1 = 0.0f;
	public float pitch2 = 0.0f;

	void Start() 
	{
		if ( !motore1.isPlaying )
		{
			motore1.Play();
		}
		motore1.loop = true;

		if ( !motore2.isPlaying )
		{
			motore2.Play();
		}
		motore2.loop = true;
	}
	
	// Update is called once per frame
	void Update() 
	{
		// forward button
		if ( Input.GetKey(KeyCode.A) )
		{
			engineRevs += engineSpeed * Time.deltaTime;
		}
		
		// backward button
		if ( Input.GetKey(KeyCode.D) )
		{
			engineRevs -= engineSpeed * Time.deltaTime;
		}
		
		// button to lower both pitches
		if ( Input.GetKey(KeyCode.S) )
		{
			// send engineRevs to zero
			// determine if stopping from forward or backwards
			if ( engineRevs > 0 ) // forward
			{
				engineRevs -= engineSpeed * Time.deltaTime;
			}
			else // backward
			{
				engineRevs += engineSpeed * Time.deltaTime;
			}
		}
		
		// clamp value
		engineRevs = Mathf.Clamp( engineRevs, -engineLimit, engineLimit );
		
		// --
		
		// set both pitches based on revs
		
		// going forward or backwards?
		if ( engineRevs > 0 ) // forward
		{
			pitch1 = Mathf.Clamp( engineRevs * maxSpeed, minSpeed, maxSpeed );
			pitch2 = minSpeed;
		}
		else // backward
		{
			pitch1 = minSpeed;
			pitch2 = Mathf.Clamp( -engineRevs * maxSpeed, minSpeed, maxSpeed );
		}
		
		// --
		
		// set pitches on both audioSources
		motore1.pitch = pitch1;
		motore2.pitch = pitch2;
	}
}

I’m not seeing where you play the clips in your update()? Do you want both clips to play at once or each to play separately?

Volume wise, I'm guessing that you could maintain two volume vars. One for Motore1 (Motore1_Vol) and one for Motore2 (Motore2_Vol). Volume is a float value between 0.0 and 1.0.

Then when you press the A you can decrement the Motore2_Vol or set it to 0.0f to be off, but this should not matter if you are setting audio.volume = Motore1_Vol; and loading and playing one clip at a time (see code below).

Assuming that you are playing one clip at a time then to play either clip just load the volume you want for that clip when you set the clip to audio.clip and set the pitch then call audio.play(). Yes?

Hope I'm making sense. Let me know if this is what you are trying to do.


    `
    // Initialize Motore1_Vol and Motore2_Vol in Start or when you define them.
    
    
    if (Input.GetKey(KeyCode.A))
    {
       audio.clip = Motore1;
       soundPitch+= 0.09f;
       audio.pitch = (soundPitch);
       Motore2_Vol = 0.0; // voll 2 is off - but I don't see why this would matter if you are only playing Motorel1 and setting it's volume seperately?
       audio.volume = Motore1_Vol;
       audio.play(); // call this assuming you want to play the clip now.
    }
     
     
     
    if (Input.GetKey (KeyCode.D))
    {
       audio.clip = Motore2;
       soundPitch+= 0.09f;
       audio.pitch = (soundPitch);
       audio.volume = Motore2_Vol;
       audio.play(); // call this assuming you want to play the clip now and the other clip above with it's own volume, pitch etc...
    }
    
    // you could just call audio.play() here once and it would use whatever settings you did above.
    // Note that this would play one or the other clip not both.

`

Change the volume before you switch to the other audio clip.

Maybe have an array of audio clips and their respective volumes like

public Audioclip[] clips = [clip1, clip2];
public float[] volumes = [vol1, vol2];
public int clipState = 0
//you could also do stuff for pitch and whatnot if you'd like, same idea - just an array, or an multidimensional array where each element contains (clip, volume, pitch, etc)

Then when you need to switch volumes and stuff:

if(Input.GetKeyDown(KeyCode.A){
clipState = 1;
audio.volume = volumes[clipState];
audio.clip = clips[clipState];
audio.Play();
}

edit: please excuse my terrible syntax, I use UnityScript so I always just sort of wing it when I do the C# stuff ha ha. Feel free to correct me so I know for the future.