The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

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():
    look_at(get_global_mouse_position())
    velocity = Vector2()
    if Input.is_action_pressed('left_click'):
        velocity = Vector2(speed, 0).rotated(rotation)

func _process(delta):
    get_input()
    velocity = move_and_slide(velocity)
in Engine by (24 points)

1 Answer

0 votes

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)
else:
    speed = lerp(speed, 0, get_process_delta_time()*friction)
velocity = Vector2(speed, 0).rotated(rotation)
by (3,229 points)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.