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