Applying acceleration and friction on a player that rotates to follow the mouse?

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

Hi. I have a player that rotates to look at the mouse, with left click to move. I’ve seen tons of examples, but this one is different: it uses one button and rotation. How should I apply acceleration and friction to my player?

extends KinematicBody2D

var screen_size 

func _ready():
	screen_size = get_viewport_rect().size

export (int) var speed = 300
var friction = 0.05
var acceleration = 0.1

var velocity = Vector2()

func get_input():
	velocity = Vector2()
	if Input.is_action_pressed('left_click'):
		velocity = Vector2(speed, 0).rotated(rotation)

func _process(delta):
	velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: Magso

You need to lerp the speed to a max speed and back to zero.

if Input.is_action_pressed('left_click'):
    speed = lerp(speed, max_speed, get_process_delta_time()*acceleration)
    speed = lerp(speed, 0, get_process_delta_time()*friction)
velocity = Vector2(speed, 0).rotated(rotation)

Thanks. I forgot to self-solve this a while ago. I ended up using linear_interpolate(). Now I have more frustrating issues… lol. But I’ll get it!

Pluto1 | 2020-11-10 18:25