Why do the enemys always go to a werid position when they follow the character?

Script: `extends CharacterBody2D

@onready var progress_bar: ProgressBar = $ProgressBar
@onready var player = get_tree().get_root().get_node(“/root/Game/Player”)
var speed: float = 0.01
var Player_pos
var Target_pos

Called when the node enters the scene tree for the first time.

func _ready() → void:
progress_bar.value = Global.enemyHits

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
progress_bar.value = Global.enemyHits
look_at(player.global_position)
position = lerp(position,player.global_position,speed)

#func _physics_process(delta: float) → void:
#Player_pos = player.global_position
#Target_pos = (Player_pos - position).normalized()

#if position.distance_to(Player_pos) > 3:
#	position += Target_pos * speed * delta

`
I have it so the enemy follow the character but when they stop moving becuase they are at me they move to a position near the corner.

I would bet your enemy or player are not centered at their scene’s origin, could you show the enemy and player scene? Your math is also mixing global_position and position which could be problematic if your main scene is offset from it’s origin.

lerp is probably a bad function to call, especially on a CharacterBody2D. Your physics process function was closer to a good script, just make sure to use velocity and move_and_slide() as it’s a CharacterBody2D

func _physics_process(delta: float) -> void:
	var difference: Vector3 = player.global_position - global_position
	var direction: Vector3 = difference.normalized()

	if difference.length() > 3:
		velocity = direction * speed
	else:
		velocity = Vector3.ZERO

	move_and_slide()

Make sure to format your pastes

2 Likes


btw most sprites are placeholders.