Navigation Offset when target reached

Godot 4.4
This may be a ‘dumb question’, but I have an enemy chasing the player but it is constantly trying to reach the origin point of the player, so when standing still the enemy and player are colliding, is there a way to set/create a form of off-set so that when the enemy is within a certain distance away of the destination it stops?

Sure. It would have been helpful if you had shared your code, but since it’s not available I’ll just speak generally about it.

I imagine in your enemy code (probably in _process(delta)), you have code that looks like

    velocity = player.global_position - global_position
    move_and_slide()

if the enemy is a physics body, or otherwise something like

    var direction_to_player = player.global_position - global_position
    global_position -= direction_to_player * speed * delta

Well, you can just condition this on the distance to the player. For example (using the second code snippet):

    var distance_to_player = global_position.distance_to(player.global_position)
    if distance_to_player > 50:
        var direction_to_player = player.global_position - global_position
        global_position -= direction_to_player * speed * delta
1 Like

I was at work when posting, sorry and thank you for the response. I did think it may be accomplished with a conditional wasn’t sure if I was missing something easy in navigation agent or some other node I didn’t know about.

Ah, for some reason I didn’t realize you were talking about a navigation agent. Maybe this property would do the trick? I’d try it myself but the computer I’m currently sitting at doesn’t have Godot. If you try it and it doesn’t work, I’m happy to look into it tomorrow

1 Like

@Ryan_Flynn I tried that property, however I’m using the nav_agent for another part of the code where the enemy is patrolling between points so when i adjusted it, it would only effect when it reached the next point in its patrolling path. I did use the conditional statement and it seems to be working, but there are the typical unexpected results. It will pursue the player when close but i just move around a bit and it seems to pause and stop intermittently, like something isn’t updating, Ill need to debug it some more when I get a chance. Again thanks for the help!