0 votes

I need to add sound to my car in my 3d car game but I don't know how to add sound.

So how do I adjust the pitch of the sound? For example, if I do it according to the engine speed of the vehicle, how will I calculate the engine speed of the vehicle? Please help me!

Godot version v3.4.4.stable.official [419e713a2]
in Engine by (92 points)

Adjust the speed of the car according to the "Volume" of "AudioStreamPlayer".
Example: If the max speed of the car is 100 (100 / 4 = 25)

When you say maximum speed, do you mean the current speed of the vehicle?

Here is the code I use for my vehicle:

var maximum_speed_it_can
var speed_kmh
var MAX_ENGINE_FORCE = 200.0

func _physics_process(delta):
    var steer_val = 0.0
    var throttle_val = 0.0
    var brake_val = 0.0

    if Input.is_action_pressed("forward"):
        throttle_val = 1.0
    elif Input.is_action_pressed("brake"):
        brake_val = 0.5

    if Input.is_action_pressed("left"):
        steer_val = 1.0
    elif Input.is_action_pressed("right"):
        steer_val = -1.0

    engine_force = throttle_val * MAX_ENGINE_FORCE
    brake = brake_val * MAX_BRAKE
    steering = lerp(steering, steer_val * MAX_STEERING, STEERING_SPEED * delta)

To find the speed of my vehicle:

    speed_kmh = linear_velocity.lenght()

And to find the maximum speed it can do:

    maximum_speed_it_can = int(MAX_ENGINE_FORCE / 0.5567928730512249)

This gives me approximately the maximum speed the car can actually do.

1 Answer

0 votes

I found this suggestion - so maybe if you combine that answer with the comment above, you will have:

  1. Vehicle speed; use to calculate a factor that makes the pitch change sound good.
  2. The AudioStreamPlayer which plays the sound; change the pitch_scale to make the sound play slower or faster.

Does that help?

by (42 points)

I understand what you mean, but how can I do it? So, I don't know how to set the pitch scale of AudioStreamPlayer according to what my car is. If you could learn the engine speed of my car, I would adjust the pitch scale accordingly, but I can't find the engine speed of the car either.

var MAX_ENGINE_FORCE = 200.0

var sound_db : float

func _process(delta):
           #sound_db = MAX_ENGINE_FORCE / 8 ## max volume_db 25
           #correcting
           sound_db = speed_kmh / 8 ## max volume_db 25
           $AudioStreamPlayer.set_volume_db(sound_db)

The speed you have in the previous answer?

How? I do not understand.
But I can't say that the above code does not exist. I need a realistic sound setting according to the engine speed of the vehicle. In other words, the sound will change according to the engine speed of the vehicle.

If you want to go "all realistic" - then changing the pitch might not be what you want. I think you'll have to change the actual sound that is playing.. and have a bunch of audio loops to pick from.

The hardest thing for me right now is to find out the engine speed of the vehicle. After learning this, I can adjust the sounds according to the engine speed. If you know how to get my car's engine speed please help.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.