Car Gear Changing

Ok. In this topic I want to make the gear change is smooth like from gear 1 to gear 2

this is my script:

                local CurrentPitch = math.abs(carPrim.Velocity.Magnitude)

				if carPrim.Velocity.Magnitude > 1  then
					seatPart.Idle.Pitch = 0.75 + CurrentPitch/gear.Gear1
				end
				
				if carPrim.Velocity.Magnitude > 100  then
					seatPart.Idle.Pitch = 0.7 + CurrentPitch/gear.Gear2
				end
				
				if carPrim.Velocity.Magnitude > 200 then
					seatPart.Idle.Pitch = 0.7 + CurrentPitch/gear.Gear3
				end
				
				if carPrim.Velocity.Magnitude > 300 then
					seatPart.Idle.Pitch = 0.7 + CurrentPitch/gear.Gear4
				end
				
				if carPrim.Velocity.Magnitude > 400 then
					seatPart.Idle.Pitch = 0.7 + CurrentPitch/gear.Gear5
				end

This isn’t the full script btw

CarPrim = car chassis (or base, for short)
seatPart = vehicleSeat that the humanoid is seatPart to
The engine sound (idle) is inside the seatPart

Solution is really appreciated.

So the issue (I assume) is that the change in pitch is too sudden. The easiest way to deal with this is to use “lerping” (linear interpolation) to ease towards the desired value.

For example, the following code running every frame:

if velocity > 0 then
    pitch = 1
else
    pitch = 0
end

Can be made more gradual by using lerping:

local target_pitch
-- ...
if velocity > 0 then
    target_pitch = 1
else
    target_pitch = 0
end
pitch = Lerp(pitch, target_pitch, 0.1)

Lerp can be defined as follows:

function Lerp(A : number, B : number, T : number) : number
    return A * (1 - T) + B * T
end
1 Like

did you mean I have to do that EVERYTIME the gear changes?
here i show the gears

local gear = {
	Gear1 = 100,
	Gear2 = 250,
	Gear3 = 325,
	Gear4 = 450,
	Gear5 = 600
}

Every time the gear changes the car sound get slower to each target. I guess that’s how it works?

theres an orange line below the lerp

i want to be like this


but smoother