How to make knockback with kinematic Body 2D?

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

Can you help a newbie?
I watched similar questions, but nothing worked
I have a function that should push the player, but it pushes him away only by Y and by X teleports him

It’s all in one player script

func _on_hitbox_body_entered(body):
	if body.is_in_group('enemy'):
		var collision_point = - body.global_position
		vel.x = sign(collision_point.x) * (speed * 20)
		vel.y = -300
		vel = move_and_slide(vel, vtop)

Maybe this will help
I use this kind of motion mechanics:

const vtop = Vector2 (0, -1)
var vel = Vector2()
export (int) var speed = 100

func _physics_process(delta):
    if Input.is_action_pressed('ui_left'):
		vel.x = -speed 
	elif Input.is_action_pressed('ui_right'):
		vel.x = speed
	else:
		vel.x = 0
    vel = move_and_slide(vel, vtop)

I understood my problem but I can not solve it.
How can I replace this part of the line?

 else:
        vel.x = 0

Thanks to everyone who helps, sorry for my english.

hello, did you ever find an answer for why it was like this I’m having the same issue right now and it’s driving me insane

RequiemByte | 2021-09-26 14:53