car engine sound template

this is basically just a template that has a cube(acting as a car) that changes the pitch of an engine sound as its velocity increases. the way to set it up is all in the script.
UPDATE 06/02/2011:
New script uses gears:

#######ENGINE SOUND SCRIPT by qazwsx2541(kendrick2541)########
# The object that this script is attached to, must have the following properties:
# -NAME-     -TYPE-      -VALUE-
# "speed"    "INT"        "0"
# "gear"     "INT"        "1"
# "maxspeed" "INT" (enter the desired max speed of your vehicle)
#
# A sound actuator named "sound" must also be attached to this controller
# the sound actuator must have an engine sound assined to it
#
################# ©2011 PowerSurgeGames ########################



##### Get Required Information #####
import GameLogic
controller = GameLogic.getCurrentController()
obj = controller.owner
pointVel = obj.getLinearVelocity(True)
obj['speed'] = pointVel[1] * 4
gear = obj['gear']
speed = obj['speed'] / 6
max = obj['maxspeed']
rat1 = max / 20
rat2 = max / 13
rat3 = max / 9
rat4 = max / 5
rat5 = max / 2


##### GEARS #####
if speed < rat1:
    gear = 1
if speed > rat1 and speed < rat2:
    gear = 2
if speed > rat2 and speed < rat3:
    gear = 3
if speed > rat3 and speed < rat4:
    gear = 4
if speed > rat4 and speed < rat5:
    gear = 5
obj['gear'] = gear

##### Use Values to change sound pitch #####
gear = gear * 10
speed = float(obj['speed']) / gear
sound = controller.actuators['sound']
sound.pitch = speed * 1.2
if sound.pitch < 1:
    sound.pitch = 1
controller.activate(sound)

Attachments

Sound.blend (460 KB)

Well, very cool, this is very usefull!, thanks!!!

Thank you, this is very useful! I wonder if it can be easily linked to engine RPM instead of car velocity, for cars that have gears.

Cool! I made something like this without python.

yes, i actually already did that, i’ll update the link to a new file as soon as i can find the file :slight_smile:

Nice! And thanks very much :slight_smile:

To start with this didnt work for me in 2.57b, but I changed


cont = GameLogic.getCurrentController()
own = cont.owner

to


import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner

and it worked. Hopefully this might prove useful to people like me who fear snakes.

Yes, you only need to add “import GameLogic” at the beginning to make this work in 2.57. Glad it’s wasn’t hard to adapt like other scripts :slight_smile:

BUMP!!! hehe

UPDATED SCRIPT, LOOK AT FIRST POST!!

oh and enjoy!

Thanks for the update! Should be very useful :slight_smile:

Script not working on 2.58

I get the following errors:

Gear, Maxspeed not defined. Plus you need to add ‘import GameLogic’ at the start.

you need to add to the script in ver 2.68a
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner

I haven’t seen this in a long time. I added the “Import GameLogic” bit at the top, but really this whole script should be updated.

Updated Script worked
" a single line make my day" “import Gamelogic”.
THANKS…

Hi, First of al thank you for te script, very usefull. I like to ask, can you give me an advice, how can I add a start engine button to this script, or to my project. I mean, I like make my car, like in real life. First you have to start the engine and then hit the gas :smiley:
I’m new in Python.
Thx for you work.

Hi, First of all, thank you for your work. I like to ask, can us give me an advice, how can I make my car using this script, adding a start engine button for start the engine first?
I’am new with Python…

You could use states (in logic bricks) have one state that waits for engine start (possibly plays a start engine sound). Another state would play the engine sounds along with any other thing that needs to happen only after the engine starts (like movement).

I would assume that there are plenty of tutorials about states, so it shouldn’t be to hard to accomplish what you want.

thanks for your reply. :smiley: will try it.

can i make a minimum pitch level? When my car starts to ride it starts with pitch: -12 but I want to change that to -5 or something. So that it will never pitch lower than -5.