//the car has either an idle sound or a gear sound, depending on the forward movement speed
//this works without the else statement, but then the sound only happens when the car is moving
//any help is very appreciate
var carIdle:AudioClip;
var carGear:AudioClip;
var forwardSpeed: float = 50;
function Update ()
{
if ((Input.GetAxis ("Vertical") * forwardSpeed) < 30)
{
audio.clip = carGear;
audio.Play();
audio.loop = true;
}
else
{
audio.clip = carIdle;
audio.Play();
audio.loop = true;
}
}
Your code says if the vertical input * speed is less than 30 you want to play the “gear” clip, otherwise if it’s equal to or greater than 30, play the “idle” clip? Isn’t that the wrong way around?
Also, if there’s lines of code that are the same whichever branch of an if/else statement is true, you should move them out of the if block - you’ll find it easier to see the logic of what your code is doing. Try this:
var carIdle:AudioClip;
var carGear:AudioClip;
var forwardSpeed: float = 50;
function Update ()
{
if ((Input.GetAxis ("Vertical") * forwardSpeed) > 30)
{
audio.clip = carGear;
}
else
{
audio.clip = carIdle;
}
// Only start the audio playing if it isn't already playing
if (!audio.isPlaying) {
audio.Play();
audio.loop = true;
}
}
Thank you!
That works great. And very helpful insight on the if else statement.
Something must be backwards elsewhere in the game because the less than actually
produces the right effect – higher louder sound for the car moving.
Thanks again