How to make player moving constantly towards direction x at the start of the game without any input?

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

I’m trying to create the same effect of the game downwell, but instead of constantly going down the player will constantly move to the right, this is what I have so far.

extends KinematicBody2D

const FRICTION = 25
const ACCELERATION = 25
const MAX_SPEED = 250
var velocity = Vector2.ZERO

func _physics_process(delta):

var input_vector = Vector2.ZERO

input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()

if input_vector != Vector2.ZERO:
	velocity += input_vector * ACCELERATION * delta
	velocity = velocity.clamped(MAX_SPEED * delta)
else:
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

move_and_collide(velocity)
:bust_in_silhouette: Reply From: Dragon20C

I think it should be simply:

velocity.x += speed

and put it into the physics process

Hi there, thanks a lot first of all, it worked. Few issues arose after but ill try and figure it out myself first. I’m new to coding but I will keep trying.

stanleylong | 2020-11-05 15:54

:bust_in_silhouette: Reply From: Surtarso
func _ready():
    velocity.x = ACCELERATION

should start moving when the node is drawn, but it will be read only once, so in that case it would move 25 to the right, if you want to keep increasing that speed it should be in the physics process funcion

func _physics_process():
    velocity.x += ACCELERATION if velocty.x < MAX_SPEED else 0

but that would also keep your node constantly at max speed after it is reached so you would have to add a timer or a toggle to disable that function after it fufilled your game design.