Hi,
I’m making a simple engine sound for my game using several AudioSources with each their own “gear”. There’s three sound clips assigned to their own AudioSources: idle, speed and speed 2.
When I press nothing, the idle sound comes on.
When I press “W” the speed sound comes on.
When I hold “W” and press and hold “LeftShift” speed2 comes on.
I’ve made my script so that when “W” is pressed the AudioSource with the speed sound on it, changes it’s pitch with a Mathf.Lerp to simulate revving up.
The same goes for when I activate speed 2, but my problem is that it’s become quite a puzzle to figure out how to rev down again when I release “Leftshift” and “W” is still held, because math.Lerp doesn’t work on KeyUp.
Here’s the code
if (Input.GetKeyUp(KeyCode.LeftShift))
{
time += 200f * Time.deltaTime;
float pitch = Mathf.Lerp(1.5f, 1f, Time.deltaTime * time);
speed.pitch = pitch;
}
Ideally I’d like for the maths.Lerp to complete it’s pitch shift over time but it can’t because it’s set to GetKeyUp, but how else could I let the game know that I’m still holding down “W” but I’ve just let go of “Lshift” and it’s now and only now that the sound associated with “W” i.e. “speed” should rev down?
I really hope this makes sense I look forward to hearing your solutions. I’m still scratching my head…
I might as well post my whole code, I think my problem makes more sense then:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EngineBeneathSound : MonoBehaviour {
//Rigidbody rb;
public AudioSource[] sounds;
public AudioSource idle;
public AudioSource speed;
public AudioSource speed2;
float time;
float time2;
float time3;
void Start () {
//rb = GetComponent<Rigidbody>();
sounds = GetComponents<AudioSource>();
idle = sounds[0];
speed = sounds[1];
speed2 = sounds[2];
if (Input.GetKey(KeyCode.W))
{
idle.Stop();
speed.Play();
}
else
idle.Play();
}
void Update () {
//if (rb.transform.position.y <= 0 ) {
if (Input.GetKey(KeyCode.W))
{
time += 100f * Time.deltaTime;
float pitch = Mathf.Lerp(.7f, 1f, Time.deltaTime * time);
speed.pitch = pitch;
time2 = 0;
}
if (!Input.GetKey(KeyCode.W))
{
time2 += 230f * Time.deltaTime;
float pitch = Mathf.Lerp(1.5f, 1f, Time.deltaTime * time2);
idle.pitch = pitch;
}
if (Input.GetKeyDown(KeyCode.W))
{
idle.Stop();
speed.Play();
}
if (Input.GetKeyUp(KeyCode.W))
{
time = 0;
time3 = 0;
idle.Play();
speed.Stop();
}
if (Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift))
{
speed.Stop();
speed2.Play();
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
{
time3 += 200f * Time.deltaTime;
float pitch = Mathf.Lerp(.7f, 1f, Time.deltaTime * time3);
speed2.pitch = pitch;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
time += 200f * Time.deltaTime;
float pitch = Mathf.Lerp(1.5f, 1f, Time.deltaTime * time);
speed.pitch = pitch;
}
if (Input.GetKey(KeyCode.W) && Input.GetKeyUp(KeyCode.LeftShift))
{
speed.Play();
speed2.Stop();
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed2.Stop();
time3 = 0;
}
if (Input.GetKeyDown(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
{
speed.Stop();
speed2.Play();
}
if (Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.W))
{
speed2.Stop();
}
//if (!Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.LeftShift))
//{
// speed.Play();
//speed2.Stop();
//}
}
}