I coded a simple 2d movement for my player but player wont response correctly

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

Hey frds please help me, I coded a movement for 2d
Player the code given below,

Extends kinematic body2d
export (int) var speed = 200

var velocity = Vector2()

func get_input():
velocity = Vector2()
if Input.is_action_pressed(“right”):
velocity.x += 1
if Input.is_action_pressed(“left”):
velocity.x -= 1
if Input.is_action_pressed(“down”):
velocity.y += 1
if Input.is_action_pressed(“up”):
velocity.y -= 1
velocity = velocity.normalized() * speed

func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)

it works fine but this few days
The player not take the input properly I give. up, down, left
Right direction But it move only one direction I tried
Copy and paste the code from documentation it won’t work I don’t know
What to do, it’s a code bug or my fault pls help

:bust_in_silhouette: Reply From: AndrewD

You have some mistaces in your code:

  1. for for checking which button pressed better to use _input(event) function. So all in getinput() paste in _input(event)
  2. you write ‘velocity.y += 1’ and call it in _process() function it’s eqevalent to 'velocity.y = velocity.y + 1
    So instead use ‘velocity.y = 1’ and ‘velocity.y = -1’. Plus and minus before equal mean addition, not assignment.