How to substitute "position" for "velocity" in a script

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

Im trying to learn from this tutorial https://www.youtube.com/watch?v=mAbG8Oi-SvQ&t=388s and at 23:55 is where im having trouble, hes able to substitute “position.x” with “velocity.x” and im unable to move in the demo/test when i do it. My script below

extends KinematicBody2D

var velocity = Vector2.ZERO

func _physics_process(delta):
if Input.is_action_pressed(“ui_right”):
position.x += 4
if Input.is_action_pressed(“ui_left”):
position.x += -4
if Input.is_action_pressed(“ui_up”):
position.y += -4
if Input.is_action_pressed(“ui_down”):
position.y += 4
indents and underscores are there, it works fine when i use “position.x”

:bust_in_silhouette: Reply From: SnapCracklins

Are you missing your move_and_collide(delta) in there?

It won’t move unless you use that. Also, I don’t see where they use “position” in the video. They are using velocity the whole time. (Honestly, you really don’t need the hard coding for position. Vector2 math - that velocity variable - already does this for you, as it does in the video.)