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

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gangster53 YT

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!

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)

ramazan | 2022-10-10 08:21

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

Gangster53 YT | 2022-10-10 12:14

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.

Gangster53 YT | 2022-10-10 12:32

:bust_in_silhouette: Reply From: Gatada

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?

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.

Gangster53 YT | 2022-10-11 13:05

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)

ramazan | 2022-10-12 09:55

The speed you have in the previous answer?

Gatada | 2022-10-12 18:08

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.

Gangster53 YT | 2022-10-12 20:20

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.

Gatada | 2022-10-14 10:55

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.

Gangster53 YT | 2022-10-14 14:22