The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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

in Engine by (79 points)

2 Answers

0 votes

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

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

by (7,890 points)

Is to long. I Need sometingh like this:

extends Area2D

member variables here, example:

var motion
const SPEED = 250

func ready():

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)


but for kinematicbody2d

0 votes

I solved:

# Codigo Jugador

extends KinematicBody2D

var semueve = false
var motion = Vector2(0,0)
var start
pos = Vector2(0,0)
const SPEED = 290.0

func ready():
pos = get_pos()

func fixedprocess(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

by (79 points)

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

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.