Enemy sticks to player

Godot Version

4

Question

my enemy gets stuck to the player when they colide from a certain angle and the player cant get rid of it.

player code:
extends CharacterBody2D

@onready var animated_sprite_2d: AnimatedSprite2D = $Area2D/AnimatedSprite2D
const SPEED = 100

var isAttaking=false

func get_input ():
var input_direction := Input.get_vector(“ui_left”, “ui_right”,“ui_up”,“ui_down”)
velocity=input_direction*SPEED

if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.x>1 || velocity.x<-1):
	isAttaking=true
	animated_sprite_2d.animation="slice"
	await get_tree().create_timer(0.25).timeout
	isAttaking=false
if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.y<-1):
	isAttaking=true
	animated_sprite_2d.animation="slice up"
	await get_tree().create_timer(0.25).timeout
	isAttaking=false
if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.y>1 ||velocity.y==0 && velocity.x==0):
	isAttaking=true
	animated_sprite_2d.animation="slice down"
	await get_tree().create_timer(0.25).timeout
	isAttaking=false

	
	
if (velocity.x>1 || velocity.x<-1) and isAttaking==false:
	animated_sprite_2d.animation="move side"
else :
	if (velocity.y<-1) and isAttaking==false:
		animated_sprite_2d.animation="move up"
	if (velocity.y>1) and isAttaking==false:
		animated_sprite_2d.animation="move down"
	if (velocity.x==0 && velocity.y==0)and isAttaking==false:
		animated_sprite_2d.animation="default"

func _physics_process(delta):
get_input()
move_and_slide()

var isLeft = velocity.x<0
animated_sprite_2d.flip_h = isLeft

the enemy should be slower but it sticks to the player for some reson.

thx

If the enemy sticks to the player from above, then the enemy may consider the player a “Moving Platform”. You can set the enemy’s floor layers to not include the player, or set their motion mode to “Floating” if you do not need floors/ceiling detection.

2 Likes

that was very helpful thank you.