Hi,
Your direction
value is always increasing, so the object will always keep moving. I think move_and_slide
already accounts for delta
, so you don't need that.
Also, you're repeating your if
statements to check for movement, which I don't think you need to do.
If you want to check for 4 way movement, the elif
's need to be if
's.
This works for me:
extends KinematicBody2D
export var speed = 100
var velocity = Vector2()
var direction = Vector2()
func _ready() -> void:
pass
func _physics_process(delta: float) -> void:
velocity = Vector2(0, 0)
if Input.is_action_pressed("ui_up"):
velocity.y += -1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x += -1
velocity = velocity.normalized() * speed
move_and_slide(velocity)