I’m trying to add audio to a tank. I’m working on a engine sound effect that will increase in pitch whenever the player accelerates. Then when they deccelerate, the pitch will gradually decrease once the player stops holding down the button to go forward. In my case, when they go forward (w), I want the pitch to increases from .5 to 1 in about 3 seconds. Then decrease back to 0.5 in 3 seconds as well.
With this code, when I press the w key, the pitch immediatly goes from 0.5 to 1 without any gradual change.
function Update {
if(Input.GetKeyDown("w")){
audio.pitch = Mathf.Lerp(0.5,1,Time.time);
}
}
as a side note, I have a audio source attached to the object with this script using the engine sound effect. Although, how would I reference the engine sound if I have more than one audio source component?
For the lerp function, you need to know that the time part works between 0 and 1 so you can use fraction if you want a precise timed lerp:
audio.pitch = Mathf.Lerp(0.5,1,(Time.time-(Time.time-startTime)/duration); → you can set the duration and when the fraction become equal to 1 the lerp is at max.
if you want to link the lerp with the speed you can calculate the current percentage of you speed 25% → speedPerc = 0.25f
audio.pitch = Mathf.Lerp(0.5,1,speedPerc); but in that case you can strait link up the current pitch like that audio.pitch = PitchMax * speedPerc; and you can set up the pitchmax, you can also work with an range… Plenty of ways to do it really…
I never use the pitch for cars, but i’m using lerps a lot so i hope it’s helpfull
is startTime a command or something? I cant find it in the scripting reference. Also why are there - signs, and how come Time.time is enclosed in brackets, and why is Time.time-startTime in
brackets??.
as for this example
audio.pitch = Mathf.Lerp(0.5,1,speedPerc);
I’m not sure what your showing because all I see different is a variable called speedPerc
I’m very noob at scripting. Could you provide a working code for these examples so I can check to see if it works?
Legend, I tried your script but it actually did the same thing as mine. I filled in the values for maxSpeed and currentSpeed but when I press w it immediatly jumps to a value with no gradual change.
I made this script that half way works. When I hold down W it starts to gradually increase the pitch. Although, I have not figured out yet how to set pitch limits so the pitch can go up infinitly.
I want the pitch to decrease gradually when the w key is not being pressed, how can I do this? Right now it just immediatly drops.
The code works. Although i’m having a struggle to understand it. Could you explain?
One thing I noticed is when the tank falls off a cliff or is free falling, the engine revs up and goes to maximum pitch. How can I change it so the pitch will only respond to the engine and not respond to velocity like falling or sliding?
I’m not sure what your referring to. It appears to be like code to handle 3D sound or something, but right now im trying to work on the tanks pitch for the engine to make it increase the faster the tank goes, and decrease when it starts to slow down. So far legends script works but when the tank falls or slides, the engine pitch increases.