# How to move a KinematicBody2D with Keyboard Left and Right

Attention Topic was automatically imported from the old Question2Answer platform.
Old Version Published before Godot 3 was released.

How to move a KinematicBody2D with Keyboard Left and Right.

example: I press the key: Left move to left
I Press the key: Right move to right

Many things of how to move a kinematic character depends on your design, but the basics are on the docs:
Official docs on Kinematic Character

And a basic (a bit old but useful) tutorial about Input and InputEvent
http://www.gamefromscratch.com/post/2015/01/28/Godot-Engine-Tutorial-Part-3-Program-Lifecycle-and-Input-Handling.aspx

Also, check the official demos on kinematic characters (there are 2 for the Stable).

Is to long. I Need sometingh like this:

``````extends Area2D
``````

# member variables here, example:

var motion
const SPEED = 250

set_process(true)
pass

func _process(delta):

``````motion = Vector2()

if Input.is_action_pressed("ui_up"):
motion += Vector2(0,-1)
if Input.is_action_pressed("ui_down"):
motion += Vector2(0,1)
if Input.is_action_pressed("ui_left"):
motion += Vector2(-1,0)
if Input.is_action_pressed("ui_right"):
motion += Vector2(1,0)

var pos = get_pos()
pos += motion * delta * SPEED
var size = get_viewport_rect().size

pos.x = clamp (pos.x,0,size.x)
pos.y = clamp (pos.y,0,size.y)

set_pos(pos)
pass
``````

but for kinematicbody2d

RetroDan007 | 2016-11-07 22:53

I solved:

``````# Codigo Jugador
``````

extends KinematicBody2D

var se_mueve = false
var motion = Vector2(0,0)
var start_pos = Vector2(0,0)
const SPEED = 290.0

set_fixed_process(true)
start_pos = get_pos()
pass

func _fixed_process(delta):

``````if Input.is_action_pressed("Tecla_Izquierda"):
se_mueve = true
motion = Vector2(-1,0)
start_pos = get_pos()

if Input.is_action_pressed("Tecla_Derecha"):
se_mueve = false
motion = Vector2(1,0)
start_pos = get_pos()

var pos = start_pos

pos += motion * delta * SPEED
set_pos(pos)

pass
``````

you should use: move instead of set_pos, set_pos will ignore collisions

MrMonk | 2016-11-08 08:10

You should mark this as the answer to your question, if it is solved

Tybobobo | 2016-11-08 21:16

You’re right “set_pos()” ignores collisions. I have problems with my game.

Sorry if some words are not understood. I’m using google translato

RetroDan007 | 2016-11-12 22:51