Enemy can reach the Player

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)

A good test is to comment out move_and_slide

If the player or the enemy is being offset by it’s parent’s transform, you could alter the direction line to use global_position instead.

func _physics_process(delta):
	if should_follow:
		player = $"../Player" # worth commenting out since `follow()` sets player
		var direction =  global_position.direction_to(player.global_position)
		velocity = direction * SPEED

If they are siblings or neither is offset by it’s parent then one or both of them are not centered, both should be at 0,0 in their own scene.

The “bounce” is because velocity does overshoot the very nearby target for each frame, you could do some extra math to solve this, or once the offsets are fixed they should just collide properly. Probably not worth looking into for this case.

Thank you both.

It was the enemy offset the problem, your first sentence through a light on the problem @gertkeno . Appreciate the time.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.