How add variable DeAccerelation in this code? Please help

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abbos
extends KinematicBody

const speed = 10
const acceleration = 5
const gravity = 9.8
const jump = 15
const mouse_sensitivity = 0.31

onready var head = $Head
onready var camera = $Head/Camera

 var velocity = Vector3()
 var camera_x_rotation = 0

 func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

 func _input(event):
if event is InputEventMouseMotion:
	head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
	
	var x_delta = event.relative.y * mouse_sensitivity
	if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90:
		camera.rotate_x(deg2rad(-x_delta))
		camera_x_rotation += x_delta

func _process(_delta):
if Input.is_action_just_pressed("ui_cancel"):
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _physics_process(_delta):
var head_basis = head.get_global_transform().basis

var direction = Vector3()
if Input.is_key_pressed(KEY_W):
	direction -= head_basis.z
elif Input.is_key_pressed(KEY_S):
	direction += head_basis.z
elif Input.is_key_pressed(KEY_A):
	direction -= head_basis.x
elif Input.is_key_pressed(KEY_D):
	direction += head_basis.x

direction = direction.normalized()

velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)
velocity.y -= gravity

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y += jump

velocity = move_and_slide(velocity, Vector3.UP)
:bust_in_silhouette: Reply From: Lopy

i believe you want a slowdown instead of an abrupt stop when you stop moving.

For that, replace :
velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)

with :
if direction.length() == 0: //no input?
. velocity = velocity * friction
else:
. velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)

With const friction being around 0.95.