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

+3 votes

In the following script, should ACCEL (acceleration) and FRIC (friction)be multiplied by delta?

extends KinematicBody2D

# Members
##movement
const SPEED = 200
const ACCEL = 30
const FRIC = 15
var velocity : Vector2
var direction : Vector2
#----- members


func get_input_dir() -> void:
    direction.x = float(Input.is_action_pressed("move_right")) - float(Input.is_action_pressed("move_left"))
    direction.y = float(Input.is_action_pressed("move_down")) - float(Input.is_action_pressed("move_up"))
    direction = direction.normalized()

func _physics_process(delta) -> void:
    get_input_dir()
    _move()

func _move() -> void:
    if direction == Vector2.ZERO:
        velocity = velocity.move_toward(Vector2.ZERO, FRIC)
    else:
        velocity = velocity.move_toward(direction * SPEED, ACCEL)
    velocity = move_and_slide(velocity)
in Engine by (210 points)
edited by

1 Answer

+1 vote

I don't understand why you're using move_toward() in the velocity variable. Usually, the directions are added or subtracted from velocity (or some Vector2 that involves movement) and that is used in the move_and_slide() function. Btw, delta should not be factored into the calculation for move_and_side(), as noted in the documentation on the function (at least in KinematicBody2D):

move_and_slide() automatically calculates frame-based movement using delta. Do not multiply your velocity vector by delta before passing it to move_and_slide().

by (3,164 points)

Thanks, I was following a tutorial that mentioned using the move_toward() method to accelerate and decelerate. I did some testing by forcing my game down to 10 fps and the acceleration and deceleration were significantly different, so I guess that answers my question.

I ended up changing the function I used, because that one, when applying delta to friction and acceleration, made me have huge acceleration and friction numbers.

These are the key parts of my final code:

const speed : int = 500
const acceleration : float = 3.0
const friction : float = 7.5

## With in the _physics_process function

#For moving
velocity = lerp(velocity, direction * speed, acceleration * delta)

# For stopping
velocity = lerp(velocity, Vector2.ZERO, friction * delta)

# And then...
velocity = move_and_slide(velocity)

Testing out my game in different frame rates showed significant changes in acceleration and friction if delta was not applied. So you do have to apply delta to acceleration and friction if your setup is like mine.

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.