Knockback on a KinematicBody2D

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

I want to implement a system where when the player gets hit with a bullet that is fired, the player is pushed back. I’m unsure of how to implement this because I want the player to be pushed back and his movement shouldn’t override the knockback received.

Here is the code for the projectile:

extends Area2D

const SPEED = 800
var velocity = Vector2()
var direction = 1

func _ready():
	pass 
	
func start():
	position = Vector2()

func set_bullet_direction(dir):
	direction = dir
	if dir == -1:
		$AnimatedSprite.flip_h = true

func _physics_process(delta):
	velocity.x = SPEED * delta * direction
	translate(velocity)
	$AnimatedSprite.play("BulletShoot")


func _on_VisibilityNotifier2D_screen_exited():
	queue_free()


func _on_Bullet_body_entered(body):
	if "Player1" in body.name:
		#KNOCKBACK
		pass
	if "Player" in body.name:
		queue_free()

Code for the player:

extends KinematicBody2D

const UP = Vector2(0,-1)
var velocity = Vector2()

const SPEED = 400
const GRAVITY = 60
const JUMP = -900

const BULLET = preload("res://Scences/BulletPistol.tscn")

var inAir = false
var holdingWeapon = false

func _physics_process(delta):
	
	velocity.y += GRAVITY
	
	if Input.is_action_pressed("p1_right"):
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = false
		if sign($Position2D.position.x) == -1:
			$Position2D.position.x *= -1
		velocity.x = SPEED
	elif Input.is_action_pressed("p1_left"):
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = true
		if sign($Position2D.position.x) == 1:
			$Position2D.position.x *= -1
		velocity.x = -SPEED
	else:
		velocity.x = 0
		if inAir == false:
			$AnimatedSprite.play("Idle")
		
	if is_on_floor():
		inAir = false
	else:
		inAir = true
		if velocity.y < 0:
			$AnimatedSprite.play("Jump")
		elif velocity.y > 0:
			$AnimatedSprite.play("Fall")
		
	if Input.is_action_just_pressed("p1_up"):
		if inAir == false:
			velocity.y = JUMP
			inAir = true
			
	if Input.is_action_just_pressed("p1_action"):
		if holdingWeapon == true:
			var bullet = BULLET.instance()
			if sign($Position2D.position.x) == 1:
				bullet.set_bullet_direction(1)
			else:
				bullet.set_bullet_direction(-1)
			get_parent().add_child(bullet)	
			bullet.global_position = $Position2D.global_position
	
	velocity = move_and_slide(velocity, UP)
	pass
:bust_in_silhouette: Reply From: quizzcode

Hey, not a pro here,
but why don’t you add a var can_move ?

before

 if Input.is_action_pressed("p1_right"):

add :

if can_move == 1:

and ont he knock back function, update the status when the player is being knock back, and remove it when you’re done.