How can I add acceleration and deceleration to my first person controller?

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

I want my FPC to move smoother but I don’t know how to do it.

I will leave the code here:

extends KinematicBody

# Variables basicas
var velocity = Vector3()
var gravity = -30
var max_speed = 8
var mouse_sens = 0.002

# Variables para armas

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

func get_input():
	var input_dir = Vector3()
	
	# Movimiento
	if Input.is_action_pressed("move_forward"):
		input_dir += -global_transform.basis.z
	if Input.is_action_pressed("move_back"):
		input_dir += global_transform.basis.z
	if Input.is_action_pressed("strafe_left"):
		input_dir += -global_transform.basis.x
	if Input.is_action_pressed("strafe_right"):
		input_dir += global_transform.basis.x
	input_dir = input_dir.normalized()
	return input_dir

func _unhandled_input(event):
	if event is InputEventMouseMotion:
		rotate_y(-event.relative.x * mouse_sens)
		$Pivot.rotate_x(-event.relative.y * mouse_sens)
		$Pivot.rotation.x = clamp($Pivot.rotation.x, -1.2, 1.2)
	
func _physics_process(delta):
	# Gravedad
	velocity.y += gravity * delta
	var desired_velocity = get_input() * max_speed
	velocity.x = desired_velocity.x
	velocity.z = desired_velocity.z
	velocity = move_and_slide(velocity, Vector3.UP, true)
	
# warning-ignore:unused_argument
func change_gun(gun):
	pass

# warning-ignore:unused_argument
func _process(delta):
	pass
:bust_in_silhouette: Reply From: Mirolo

Hi
I would suggest using a damping system because ist a bit more realistic and also easier to Work with. Something like this should do the Trick:

velocity.x += max_speed * get_input().x
velocity.z += max_speed * get_input().z

velocity.x *= pow(1.0 - damping, delta * 10)
velocity.z *= pow(1.0 - damping, delta * 10)

This ensures thats the acceleratoon from 0 to 100 ist bigger then from 100 to 200. Of course this method also has some downsights, for example you can’t CLEANLY Control between acceleration and deceleration. This method is from
[Jonas Tyroller : 5 Reasons your Indie Platformer sucks] .
It has a lot of useful tips that I also use in my FPS Game. I can only recommend it.
I hope that I could help you. :slight_smile:

it worked, thx so much!
and also i going to look that video :)))

harden32x | 2022-09-18 18:29