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.

Does your kinematicbody2d is your “character” node? How do you know it’s not working, what do you execpt ?

UnRealCloud 1 | 2020-07-29 17:14

:bust_in_silhouette: Reply From: p7f

What is not working? collisions or movement?
So, you mapped “left” and “right” actions to some keys? cause i tried on my machine and it works fine. I used this and moved with the arrows:

extends KinematicBody2D

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

func _ready():
    pass

func getinput():
    if Input.is_action_pressed("ui_right"):
	    velocity.x = SPEED
    elif Input.is_action_pressed("ui_left"):
	    velocity.x = -SPEED
    else:
	    velocity.x = 0

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

If you want to change the velocity with the touchscreen buttons, then you should try checking if they are pressed with something like:

if get_node("TouchScreenButtonRight").is_pressed():
    velocity.x = SPEED
elif get_node("TouchScreenButtonLeft").is_pressed():
    velocity.x = -SPEED
else:
    velocity.x = 0

Obviously, replacing for your nodes names in the get_node function.

About the collisions, for KinematicBody2D to collide, those collision shapes have to be children of other bodies, like StaticBody2D, or RigidBody2D. Bodies collide with bodies, they dont collide with just shapes. Areas dont collide, but detect when a body or other area enter or exit the area.

Sorry, I forgot to tell that the collisions arent working

Criepstar | 2020-07-29 17:21

Collisions with other bodies, right? i see in the tree you showed above, you have some collision shapes and collisions polygons, but for KinematicBody2D to collide, those collisions have to be child of other bodies, like StaticBody2D, or RigidBody2D. Bodies collide with bodies, they dont collide with just shapes. Areas dont collide, but detect when a body or other area enter or exit the area.

p7f | 2020-07-29 17:32

Thanks its working now and honestly I feel a little bit stupid, because I looked at a old project and thought that this could’t be the problem. Once again thank you for your help

Criepstar | 2020-07-29 17:39

glad to help! You may select the answer so others see its solved. Ill eddit and add about the collisions.

p7f | 2020-07-29 18:17

I selected it, didn’t knew i could do this

Criepstar | 2020-07-29 21:07