I need the enemy to follow the player, but for some reason he just flies off the map

Godot Version 4.3

Godot 4.3

Question

I’m making a top-down game, I need the enemy to follow the player, but for some reason he just flies off the map, As if touching the detection_area the player pushes the enemy out, what should I do?
Script:

extends CharacterBody2D

var speed = 25
var player_chase = false
var player = null

func _physics_process(_delta):
if player_chase:
position += (player.position - position)/speed

func _on_detection_area_body_entered(_body):
player = _body
player_chase = true

func _on_detection_area_body_exited(_body):
player = null
player_chase = false

Not immediately obvious what the issue is, but I would check using global_position rather than position. If your nodes do not share a parent, funk could be occurring.

If it is just that the direction is opposite to what you want, invert it.

So he just stands still.

As in
global_position += (player.global_position - global_position)/speed

Continues to fly away :frowning:

Can you try this?

position += (position - player.position)/speed

2D top-down game

1 Like

Now the enemy just goes down the map

goes down behind the map

But why are you not using velocity? Just do like this:

if player_chase:
    velocity = (global_position - player.global_position) * speed
    move_and_slide()

Give a second try if it does not work:

global_position += (player.global_position - global_position) * speed * delta

I’ll try now, I apologize for the unclear answers, for some reason the forum is lagging a lot

1 Like

In the first case, it rests against the lower right corner of the room, in the second, it also flies up but in jerks

1 Like

Means it goes at the the opposite of the player? Or just a random dir?

Previously, the player goes to the enemy and he flies to the upper left corner from the player, but now to the lower right, behind the player, It’s a pity I can’t upload a video to make it clearer

Last Try from me:

if player_chase:
    var direction = (player.global_position - global_position).normalized()
    velocity = direction * speed
else:
    velocity = Vector2.ZERO

if global_position.distance_to(player.global_position) > 10 or global_position.distance_to(player.global_position) < -10:
    move_and_slide()

It gives an error: Invalid access to property or key ‘global_position’ on a base object of type ‘Nil’

Oh I forgot that:

if player_chase:
    var direction = (player.global_position - global_position).normalized()
    velocity = direction * speed

    if global_position.distance_to(player.global_position) > 10 or global_position.distance_to(player.global_position) < -10:
        move_and_slide()
else:
    velocity = Vector2.ZERO

Same error

1 Like

Do you removed the previous codes?

This is the whole codes in the physics process:

if player_chase:
    var direction = (player.global_position - global_position).normalized()
    velocity = direction * speed

    if global_position.distance_to(player.global_position) > 10 or global_position.distance_to(player.global_position) < -10:
        move_and_slide()
else:
    velocity = Vector2.ZERO

Да удалил, The error does not disappear