- Home /
Engine noises creation
Okay so I want to create a racing game. I know its a bit of a stupid question, and i might be asking too much. but could somebody tell me a script that would be a bit like this
If car.speed = 10 then
play sound1
if car.speed = 20 then
play sound2
abit like that, any ideas?
Answer by straydogstrut · Apr 05, 2010 at 02:18 PM
I think you're pretty much there with your pseudocode. Maybe something like this (untested):
// define variables for the car, speed and sounds // you will need to assign these in the editor var car : GameObject; var speed : int; var idleSound : AudioClip; var firstGearSound : AudioClip; var secondGearSound : AudioClip;
// initially set speed to zero function Start(){ speed = 0; }
function Update(){
if(speed==10){ // set the default audio clip to value stored in this variable audio.clip = firstGearSound;
} else if(speed == 20){ audio.clip = secondGearSound;
} else { audio.clip = idleSound; }
// play the default audio clip
audio.Play();
}
// make sure this gameobject has an audiosource attached for it to play sounds @script RequireComponent(AudioSource)
I don't drive so the first/second gear names are purely arbitrary! You'd probably want different sounds for gear changes and engine revs and it might be better to use speed ranges rather than fixed amounts. There's a detailed car tutorial here which you might find useful. I haven't actually tried switching sounds immediately like i'm suggesting above so you may find it isn't as smooth as you'd like. You might then have to look into crossfading between sounds for a smooth transition.
Good luck.
Just another question, Do you know how to make the speed change as time goes on, for example, i can only set my speed at the start, can i change it throughout? thanks!
You would just set your speed variable when something happens, like input from the player, which you look out for in the Update function. If you wanted the speed to increase continually while the key is held down, just add to the speed variable (speed++ would increment by 1 each time for example). Look at Input.Get$$anonymous$$eyDown or Input.GetAxis in the scripting reference to get you started.
I'v looked at what you told me and cant seem to understand it very well. I'm quite new to unity so I'm really a noob. I used the tutorial you told me to use but didnt have much luck. The script you sent me was a success although i wanted to create a script that would change the speed over a time span. Could you be so kind as to tell me how to do it. I want it so that the top speed is a variable, so I can change it whenever I feel like. Thankyou so much! :D
I'll be honest, i'm fairly new to Unity myself. I think it would be more helpful to you to look at some of the other threads on here which have links to car tutorials (just search for "car") since it probably won't be long before you get into rigidbodies and wheel colliders..things I know very little of. Googling just now I came across this tutorial - http://www.tutorius.net/2010/03/unity-racing-game-tutorial/ - which is specifically about creating a racing game so might be useful to you. Of course, feel free to ask further questions here, i'm sure someone will be able to help you=)
Your answer
![](https://koobas.hobune.stream/wayback/20220612144909im_/https://answers.unity.com/themes/thub/images/avi.jpg)