This is the programming for my kinematic body 2d:

extends KinematicBody2D

var motion = Vector2()

func _process(delta):
	if Input.is_action_pressed("ui_right"):
		motion.x = 100
	elif Input.is_action_pressed("ui_left"):
		motion.x = -100
	else:
		motion.x = 0
	if Input.is_action_pressed("ui_up"):
		motion.y = -100
	elif Input.is_action_pressed("ui_down"):
		motion.x = 100
	else:
		motion.y = 0

	pass
  • I do not understand what I have done here but I'm making a top-down game and when I click the arrow keys, the kinematic body 2d doesn't move (I have a sprite attached to it too).

Are you calling something like move_and_slide or move_and_collide somewhere in the script?

To move through the physics world, you will need to call either move_and_slide or move_and_collide, depending on the needs of your project and how you want the KinematicBody2D node to move.

For example, using move_and_slide:

extends KinematicBody2D
var motion = Vector2()
# You want to call move_and_slide or move_and_collide only
# in _physics_process if you can help it, so that the character moves
# with an up-to-date physics world
func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		motion.x = 100
	elif Input.is_action_pressed("ui_left"):
		motion.x = -100
	else:
		motion.x = 0
	if Input.is_action_pressed("ui_up"):
		motion.y = -100
	elif Input.is_action_pressed("ui_down"):
		motion.y = 100
	else:
		motion.y = 0
	
	# Tell the KinematicBody2D node to move through the physics
	# world, going towards the direction stored in motion
	var velocity = move_and_slide(motion)

This page on the documentation explains more about how the KinematicBody2D node works, which might be helpful if you have not seen it already.

Some other resources that might be helpful:

Disclaimer: I have not gone through any of the tutorials myself


If you want to just move the node without any collision detection, then you just need to modify the node's position/global_position so that it moves.

For example:

extends KinematicBody2D
var motion = Vector2()
# You want to call move_and_slide or move_and_collide only
# in _physics_process if you can help it, so that the character moves
# with an up-to-date physics world
func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		motion.x = 100
	elif Input.is_action_pressed("ui_left"):
		motion.x = -100
	else:
		motion.x = 0
	if Input.is_action_pressed("ui_up"):
		motion.y = -100
	elif Input.is_action_pressed("ui_down"):
		motion.y = 100
	else:
		motion.y = 0
	
	# Change the position of the node so that it moves towards
	# motion (multiplied by delta for smoother movement)
	# This will NOT take collisions into account.
	global_position += motion * delta

Hopefully this helps!

3 years later