How do Collisions with KinematicBody2D work?

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

I know, there are many tutorials out there, but no of them works at my project.
So I was trying to make an mobile game only with left and right controls.
This are my nodes:

  • Node2D
    -Node2D
    -Touchscreenbutton
    -Touchscreenbutton
    -KinematicBody2D
    -Sprite
    -CollisionPolygon2D
    -Area2D
    -CollisioShape2D
    -CollisioShape2D

My code so far:

extends KinematicBody2D

var velocity = Vector2(250,0)
const SPEED = 550


func _ready():
	pass


func get_input():
	if Input.is_action_pressed("right"):
		velocity.x = SPEED
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
	else:
		velocity.x = 0


func _physics_process(delta):
	get_input()
	move_and_collide(velocity * delta)

So why does it not work and i tried both move_and_collide and move_and_slide, but i dont know why they dont work.

Note: please use the “Code Sample” button to properly format code when posting. I’ve edited your post to fix it.

Also, that node hierarchy looks odd - I can’t really tell what is a child of what.

kidscancode | 2020-07-29 19:35