Newbie Needing Help with Acceleration and Friction

Godot Version

3.5.3

Question

Hi there!

I am fairly new to coding and Godot in general, so try and bear with me, haha.

So I am trying to create a KinematicBody2D. I want it to have an “Asteroids Style” movement with acceleration along with some friciton to slow down the character.

I am trying to work with this:

func get_input(delta):
	rotation_dir = 0
	velocity = Vector2()
	if Input.is_action_pressed("Rotate_Clockwise"):
		rotation_dir += 1
	if Input.is_action_pressed("Rotate_CounterClockwise"):
		rotation_dir -= 1
	if Input.is_action_pressed("Reverse"):
		velocity = Vector2(0, SLOW_MOVEMENT / 4).rotated(rotation)
	if Input.is_action_pressed("Thrust"):
		velocity = Vector2(0, -SLOW_MOVEMENT).rotated(rotation)
		
	else:
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

func rotation(delta):
	rotation += rotation_dir * ROTATION_SPEED * delta

func move():
	velocity = move_and_slide(velocity)

func _physics_process(delta):
	get_input(delta)
	rotation(delta)
	move()

It seems that the friction is not working and I am not quite sure how to implement acceleration. Should I not be using this as my method of movement? If anyone has any recommendations as to what I should do (whether it be code, learning resources, tutorials, etc), I would be greatly appreciative.

Thanks!

tl;dr: How do implement acceleration and friction?

This resets velocity every frame, that’s why friction doesn’t work. I assume the character just stops if there’s no input? So try removing this line and see if friction works then.

As for thrust / reverse, try this:

if Input.is_action_pressed("Reverse"):
	velocity += Vector2(0, SLOW_MOVEMENT / 4).rotated(rotation) * delta
elif Input.is_action_pressed("Thrust"):
	velocity += Vector2(0, -SLOW_MOVEMENT).rotated(rotation) * delta

It might not be exactly what you want but basically if you want acceleration you have to keep adding to velocity, instead of just setting it to a fixed value.

2 Likes

I removed the velocity = Vector2() and the friction is working now!

Changing the velocity to += and multiplying by delta is exactly what I was looking for! I threw it all in parenthesis and added .limit_length() at the end to clamp the speed. ← That didn’t work, but adding velocity = velocity.limit_length() on a new line did.

I suppose my question now is, should I keep trying to progress by this “trial by fire” method? I watch tutorials and follow along and try to change little stuff here and there to get an understanding of what’s going on, but I can’t help but feel that there is an easier(?) way to learn this stuff.

Anyhow, I have been trying to figure this out for WEEKS now and it has been driving me crazy, haha. So thank you! I really appreciate it~

Glad it worked :slight_smile:

I don’t know if there’s a better way… The official documentation has tutorials “Your first 2D & 3D game” that explain the basics pretty well, though you might already know most of that stuff. Other than that it’s a lot of trial and error, figuring out stuff + looking at random places online… It can take a while for a lot of things to “click” but it’ll get easier.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.