Godot Version
4
Question
For some reason my enemy get stuck in a position and doesn’t reach the player. I’ve deactivated all the collision shapes, and commented out all the code for player and enemy(apart from the chase code). Watch the video for more information.
Player Script:
class_name Player
extends CharacterBody2D
const SPEED = 300.0
@onready var animation_player = $AnimationPlayer
@onready var character_sprite = $Sprite2D
@onready var damage = 10
func _physics_process(delta):
movement_animation()
move_and_slide()
func _input(event):
var inputClass = event.get_class()
if inputClass == 'InputEventMouseButton' or inputClass == 'InputEventKey':
player_move()
attack_animation()
func player_move():
velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * SPEED
func attack_animation():
if Input.is_action_just_released("attack"):
animation_player.play("attack")
elif Input.is_action_just_released("attack_2"):
animation_player.play("attack_2")
func movement_animation():
if not animation_player.is_playing():
animation_player.play("idle")
if Input.get_vector("move_left", "move_right", "move_up", "move_down"):
animation_player.play("walk")
if Input.is_action_pressed("move_left"):
character_sprite.scale.x = -1
elif Input.is_action_pressed("move_right"):
character_sprite.scale.x = 1
Enemy Script:
extends CharacterBody2D
@onready var health = 50
@onready var animation_player = $AnimationPlayer
@onready var character_sprite = $Sprite2D
const SPEED = 200.0
var player
var should_follow = true
func _physics_process(delta):
if should_follow:
player = $"../Player"
var direction = (player.position - position).normalized()
velocity = direction * SPEED
#movement_animation()
move_and_slide()
func take_damage(damage):
health -= damage
if health <= 0:
queue_free()
func follow(body):
should_follow = true
player = body
func movement_animation():
if not animation_player.is_playing():
animation_player.play("idle")
I’ve been trying to debug this for hours. If i print the position of player and enemy, they seem very close, but this is not what happens visually + it bounces:
player-position: (-197, 2)
enemy-position: (-196.6618, 1.998296)
player-position: (-197, 2)
enemy-position: (-199.9951, 2.015089)