Skip to content

Instantly share code, notes, and snippets.

@mxamber
Last active October 5, 2015 16:20
Show Gist options
  • Save mxamber/f99344b9535ebb7c9975 to your computer and use it in GitHub Desktop.
Save mxamber/f99344b9535ebb7c9975 to your computer and use it in GitHub Desktop.
A small script for Blender Game Engine Python to change the pitch of the car engine sound depending on the speed of the car.
def engineSound ():
from bge import logic
cont = logic.getCurrentController()
# Name of the Sound Actuator playing your sound
sound = cont.owner.actuators["enginesound"]
# Replace "Car" with the Name of your car
speed = abs(round(sum(scn.objects["Car"].linearVelocity), 2) * 0.25)
cont.owner["speed"] = speed
# Kind of "Gears", defines the speed after which the rounds-per-minute of the engine "reset"
if speed < 2:
multiplier = 0.15
# elif speed < 3:
# multiplier = 0.1
elif speed < 6:
multiplier = 0.05
elif speed < 9:
multiplier = 0.035
else:
multiplier = 0.02
# Again - replace "Car"
pitch = abs(round(sum(scn.objects["Car"].linearVelocity), 2) * multiplier)
if pitch > 1.25:
pitch = 1.25
elif pitch < 0.5:
pitch = 0.5
# Prints the pitch to the system console. Not noecessary, just for debugging
print(pitch)
sound.pitch = pitch
sound.startSound()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment