I want to make an acceleration based movement

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

I want to make a movement system where the player’s speed increases progressively until they get to top speed,so movement feels more natural.
However,instead of the player’s speed increasing progressively (although quickly) until top speed,the player reaches top speed almost instantly.

Basically,my intention was to create a system that adds the player’s speed to 10 units per frame until it got to the top speed,90 units per frame. However,the speed value goes instantly from 0 to 80 and from 80 to 90,instead of going from 0 to 10,10 to 20 and so goes on until it got to 90.

Any clue to why this happens?

The code:

var motion = Vector2()
var speed = 10
var stop_speed = 80
var top_speed1 = 90
var top_speed2 = -90
var input_left = false
var input_right = false
var input_up = false
var left
var right
var x_math
var x_direction = 1
const jump_power = -250
const gravity = 10
const shot = preload(“res://shot.tscn”)
var shot_timer = 0

func _physics_process(delta):
motion = move_and_slide(motion,motion)

var input_left = Input.is_action_pressed(“Left”)
var input_right = Input.is_action_pressed(“Right”)

if input_left:
left = 1
motion.x -= speed
left = 0
motion.x += stop_speed
if input_right:
right = 1
motion.x += speed
right = 0
motion.x -= stop_speed

if !input_left and x_direction == -1 and motion.x < 0:
motion.x = 0
if !input_right and x_direction == 1 and motion.x > 0:
motion.x = 0

if motion.x > top_speed1:
motion.x = top_speed1
if motion.x < top_speed2:
motion.x = top_speed2

:bust_in_silhouette: Reply From: JimArtificer

Using framerate to control this is a mistake. A game should run at 60 fps or higher, and you can know for certain it won’t run at the same framerate on every computer.

The reason it appears instant is likely because within a second that function was called at least 30 times.

You need to incorporate the delta parameter in your calculations. Strictly speaking acceleration is change in velocity over time. That way the amount of acceleration will be modified by how long it has been between calls to the physics process.

Doesn’t putting the delta inside the parentheses in the _physic_process function automaically add delta to every single calculation inside the function? Am I supposed to add it manually to these calculations?

Harmonikinesis | 2020-08-14 05:09