Please help, enemy not working correctly

Godot Version

Newest version

Question

Ok so I have a game, where you can place different units, then there is meant to be an enemy that will go to the position of the unit. For some odd reason, the enemy stops at the top left of the unit and not at the unit itself. Here is my enemy code:

extends Node2D

@export var body: CharacterBody2D
@export var animationplayer: AnimationPlayer
@export var speed: float = 100.0 # Speed at which the enemy moves
@export var stop_distance: float = 10  # Distance at which the enemy stops moving towards the target
@export var collision_shape: CollisionShape2D  # Assign the CollisionShape2D node from the enemy scene

var target: Node = null

# Selects a random target from the "targets" group
func select_random_target():
	var targets = get_tree().get_nodes_in_group("targets")
	if targets.size() > 0:
		target = targets[randi() % targets.size()]
		print("New target selected: ", target.name)
	else:
		target = null
		print("No targets available")

func _process(delta):
	if body != null:
		if target == null:
			select_random_target()  # Continuously check for a target
		
		if target != null:
			print("Enemy Position:", position)
			print("Target Position:", target.global_position)
			
			var distance_to_target = position.distance_to(target.global_position)
			print("Distance to Target:", distance_to_target)
			
			if distance_to_target > stop_distance:
				position = position.move_toward(target.global_position, delta * speed)
			else:
				print("Reached target.")

Here is a screenshot of the error:

Enemy

Seems like the enemy must be offset. Is it a child of something with a altered transform, or the sprites and collision shapes not centered to 0,0?

all of the sprites and collsions are at 0,0. I am using a node specifically for the enemy ai. the node tree is Node2d: Enemy > Node2d: ai > Characterbody2d > Sprite2D, CollisionShape2D

the ai node is a custom node I made, im trying to make this game modular

Try to decrease the stop_distance like set it to 1-2

does not work

the enemy just always stops at the same spot every time, always the top left corner above the target

EDIT: here is my map script if that will help:

extends Node2D
var smoke = load("res://Art/FX/smoke_particle.tscn")
var knight_place = load("res://Scenes/Characters/Knight/knight_place.tscn")
var knight = load("res://Scenes/Characters/Knight/knight.tscn")
var knight_made = false
signal knight_created
var canplace = true
var collision_count = 0
func _on_mainui_knight_pressed():
	create_knight(get_global_mouse_position())
	

func _ready():
	Signals.connect("Cant_place", self.Cantplacefunction)
	Signals.connect("Can_place", self.Canplacefunction)
	
func create_knight(mouse_pos):
	if knight_made == false:
		var new_knight = knight_place.instantiate()
		new_knight.position = mouse_pos
		add_child(new_knight)
		knight_made = true
		return new_knight
		

func knight_follow_mouse():
	knight_place = get_child(5)
	knight_place.position = get_global_mouse_position()

func save_mouse_pos():
	var mouse_pos = get_global_mouse_position()
	return mouse_pos

func _physics_process(delta):
	if knight_made == true:
		knight_follow_mouse()
		if Input.is_action_just_pressed("Place") and canplace == true:
			var new_knight = knight.instantiate()
			new_knight.position = get_local_mouse_position()
			new_knight.add_to_group("targets")
			add_child(new_knight)
			knight_created.emit()
			var new_smoke = smoke.instantiate()
			new_smoke.position = new_knight.position
			add_child(new_smoke)
			new_smoke.get_child(0).emitting = true
			$AudioStreamPlayer2D.play()
			
			
			
			


func Cantplacefunction():
	collision_count += 1
	canplace = collision_count == 0
	
func Canplacefunction():
	collision_count -= 1
	canplace = collision_count == 0

	


It would help to have images of the scene tree and the objects positions in it. Maybe post the player’s scene and the level’s scene. Your code is correct, one addition could be to use the enemies global_position, this will help unless it, or the player’s children are offset.

var distance_to_target = global_position.distance_to(target.global_position)
print("Distance to Target:", distance_to_target)

if distance_to_target > stop_distance:
	global_position = global_position.move_toward(target.global_position, delta * speed)
else:
	print("Reached target.")
1 Like

ah, it must be offset then because that seems to fix it, weird as all the nodes are at 0,0. I used this code in another project and it worked fine. well thank you so much for the fix, I was trying to wrap my head around it for a while lol

it must be the parent of the enemy, check your main/world scene. Might cause more issues, might not.

that must be it! My “Ai” node i made wasnt the parent, im still new to godot and im still learning these things lol

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