Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jabba |
Hi All,
I am trying to let the kinematicbody2d (player) make some simpel moves but I have got two problems:
1- The “player” does not moce smoothly
2- When the key for movement is released de “player” keeps moving.
I wrote the following code:
enter code here
extends KinematicBody2D
export var speed = 0
var velocity = Vector2()
var direction = Vector2()
func _ready() -> void:
pass
func _physics_process(delta: float) -> void:
var is_moving = Input.is_action_pressed("ui_down") or Input.is_action_pressed("ui_up") or Input.is_action_pressed("ui_right") or Input.is_action_pressed("ui_left")
if is_moving:
speed = 50
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
elif Input.is_action_pressed("ui_down"):
velocity.y += 1
elif Input.is_action_pressed("ui_right"):
velocity.x += 1
elif Input.is_action_pressed("ui_left"):
velocity.x -= 1
else:
speed = 0
direction += velocity * delta * speed
move_and_slide(direction)
Thank you in advance