My KinematicBody2D moves when another one of the same type collides

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

Hello everyone, I’d been following a tutorial on how to make a basic pong game but wanted to try changing the typical bars for rounded shapes.
Now I’m facing the problem that whenever the ball hits the “player” it will push them slightly each time. I’m not sure if I did something wrong with the code or if changing the shape had something to do with it.

This is the code for my “player”:

extends KinematicBody2D

var speed = 400

func _physics_process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed(“ui_up”):
velocity.y -= 1
if Input.is_action_pressed(“ui_down”):
velocity.y += 1
move_and_slide(velocity * speed)

This is the code for my ball:

extends KinematicBody2D

var speed = 600
var velocity = Vector2.ZERO

func _ready ():
velocity.x = [-1, 1][randi () % 2]
velocity.y = [-0.8, 0.8][randi () % 2]

func _physics_process(delta):
var collision_object = move_and_collide(velocity * speed * delta)
if collision_object:
velocity = velocity.bounce(collision_object.normal)

I tried several things like changing the “player” to RigidBody2D and StaticBody2D but I know this is not optimal for what I’m trying to do.

Any help is appreciated, thanks in advance.

:bust_in_silhouette: Reply From: beaverusiv

You definitely want to use a static body, read documentation here:

If you want to make more smooth movement with slide functions you could use a rigid body with axis locked also: