How do i push the player a little bit when they swing their sword?

I want the player to move forward to their looking direction when the swing their sword. How would i do this?

My full code below thanks😅:

extends CharacterBody2D

const PlayerHurtSound = preload("res://scenes/player_hurt_sound.tscn")

const ACCELERATION = 500
const MAX_SPEED = 80
const ROLL_SPEED = 105
const FRICTION = 500

#@export var Damage = 10

###########PlayerSpecialAbilities############
@export var SpecialPowerAwaken = false

###########SPWeapon#######################
@export var SPweaponEQUIPED = false
@export var SPDamage = 50

enum {
	MOVE,
	ROLL,
	ATTACK,
	spATTACK,
	LimboSLICE
}

var state = MOVE
var roll_vector = Vector2.DOWN
var stats = PlayerStats
var max_comboCount = 3
var comboCount = 0


func _ready():
	randomize()
	stats.connect("no_health", Callable(self, "queue_free"))
	swordHitbox.knockback_vector = roll_vector
	animationTree.active = true
	

@onready var animationPlayer = $AnimationPlayer2
@onready var animationTree = $AnimationTree
@onready var animationState = animationTree.get("parameters/playback")
@onready var swordHitbox = $HitboxPivot/SwordHitbox
@onready var dustparticle = $DustParticles
@onready var hurtbox = $Hurtbox
@onready var blinkAnimplayer = $AnimationPlayerBlink
@onready var playerfootsound = $PlayerSoundBag/FootStepSound
@onready var playerfootsoundtimer = $PlayerSoundBag/FootTimer
@onready var LimboTIME = $PlayerAbilityBag/LimboTime



#######################Ability variables#####################
var CANLimboslice = false

#############################################################



func _process(delta):
	pass


func _physics_process(delta):
	match state:
		MOVE:
			move_state(delta)
		ROLL:
			roll_state(delta)
		ATTACK:
			attack_state(delta)
		LimboSLICE:
			Limboslice_state(delta)
			
			
			
	##resetting combo count##
	#if comboCount >= 3:
		#comboCount = 1

func move_state(delta):
	$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	if input_vector != Vector2.ZERO:
		roll_vector = input_vector
		swordHitbox.knockback_vector = input_vector
		animationTree.set("parameters/Idle/blend_position", input_vector)
		animationTree.set("parameters/Run/blend_position", input_vector)
		animationTree.set("parameters/Attack/blend_position", input_vector)
		animationTree.set("parameters/Limboslice/blend_position", input_vector)
		animationTree.set("parameters/Secondattack/blend_position", input_vector)
		animationTree.set("parameters/Roll/blend_position", input_vector)
		animationState.travel("Run")
		dustparticle.emitting = true  #THIS IS WHERE I EMIT DUST PARTICLE
		velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
		if playerfootsoundtimer.time_left <= 0:
			playerfootsound.pitch_scale = randf_range(0.8, 1.2)
			playerfootsound.play()
			playerfootsoundtimer.start(0.2)#(0.1)
	else:
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		animationState.travel("Idle")
		dustparticle.emitting = false
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
		
	#move_and_collide(velocity * delta) #we must change this to move and slide in Heartbeast tutorial
	
	move()
	
	if Input.is_action_just_pressed("attack"):
		if comboCount >= 3:
			comboCount = 1
		else:
			comboCount += 1
		if CANLimboslice == true:
			state = LimboSLICE
		else:
			state = ATTACK
	
	if Input.is_action_just_pressed("TestLIMBO"): #and CANLimboslice == true:
		#state = LimboSLICE
		global.slowMODE = true
		print("it gotta work 0.5")

	if Input.is_action_just_pressed("roll") and PlayerStats.stamina > 0:
		#global.slowMODE = false
		print("speed up normal")
		state = ROLL
		print(PlayerStats.stamina)
		stats.stamina -= 1
		CANLimboslice = true
		global.Canlimboglobal = true
		LimboTIME.start()


func roll_state(_delta):
	velocity = roll_vector * ROLL_SPEED
	animationState.travel("Roll")
	dustparticle.emitting = true
	$Hurtbox/CollisionShape2D.set_deferred("disabled", true)
	move()

func attack_state(_delta):
	if comboCount == 1:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		velocity = Vector2.ZERO
		animationState.travel("Attack")
	elif comboCount == 2:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		velocity = Vector2.ZERO
		animationState.travel("Secondattack")
	elif comboCount == 3:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		velocity = Vector2.ZERO
		animationState.travel("Spinattack")

	
func Limboslice_state(_delta):
	$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
	velocity = Vector2.ZERO
	animationState.travel("Limboslice")

func move():
	move_and_slide()


func roll_animation_finished():
	#velocity = velocity * 0.8
	velocity = Vector2.ZERO #remove the "#" if u dont want sliding when you roll
	state = MOVE

	
func attack_animation_finished():
	state = MOVE

func pierce_animation_finished():
	state = MOVE

func Limboslice_animation_finished(): #maybe we might have to do this with a node connection
	state = MOVE

func spinattack_animation_finished():
	state = MOVE

func playerteleport():
	pass


func _on_hurtbox_area_entered(area):
	stats.health -= area.Damage
	hurtbox.start_invincibility(0.6)
	hurtbox.create_hit_effect()
	print("player health = ",stats.health)
	var playerHurtSound = PlayerHurtSound.instantiate()
	get_tree().current_scene.add_child(playerHurtSound)


func _on_hurtbox_invincibility_started():
	blinkAnimplayer.play("Start")


func _on_hurtbox_invincibility_ended():
	blinkAnimplayer.play("Stop")


func _on_stamina_adder_timeout():
	if stats.stamina < stats.max_stamina:
		stats.stamina += 1
	



func _on_limbo_time_timeout():
	global.Canlimboglobal = false
	CANLimboslice = false

#func slowtime():
#	Engine.time_scale = 0.5

#func slowtimeSTOP():
#	Engine.time_scale = 1




func _on_combocount_reset_timeout():
	comboCount = 0

func attack_state(delta):
	# Create a variable to store the push vector
	var attack_push := Vector.ZERO

	if comboCount == 1:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		# Get your current facing direction and multiply for the amount of 
		# force you want to apply on the push
		attack_push = velocity.normalized() * 200
		velocity = Vector2.ZERO
		animationState.travel("Attack")
	elif comboCount == 2:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		# Same as above
		attack_push = velocity.normalized() * 200
		velocity = Vector2.ZERO
		animationState.travel("Secondattack")
	elif comboCount == 3:
		$PlayerAbilityBag/CombocountReset.start()
		$Hurtbox/CollisionShape2D.set_deferred("disabled", false)
		# Same as above
		attack_push = velocity.normalized() * 200
		velocity = Vector2.ZERO
		animationState.travel("Spinattack")

	# Call move_and_collide to apply the force
	if attack_push.is_zero_approx() == false:
		move_and_collide(attack_push * delta)
1 Like

Hey, thanks for answering. It worked but it instantly teleports the player to the place instead of pushing them there. Is there a way to push them instead?

Ah, thanks it works 100%, i just needed to remove the velocity = Vector2.ZERO that came after attack_push = velocity.normalized() * 200. God bless you man