Enemy agro, circling, and attack help

Godot Version

4.7.2

Question

I’ve tried several tutorials for making enemies, but I can’t seem to make them work, and I want to know if anyone could help me either by linking tutorials that had work for you or your own in-depth knowledge on how gdscript works.


What kind of issues have you been encountering? What have you gotten to work, and where did you get stuck?

Are you building this in 2D or 3D? Your code block came through empty, so we can’t see where the current attempt breaks.

Start with one enemy and separate its behavior into chase → wind-up → attack → recovery. Only the current state should control movement. That prevents chase code from pulling the enemy forward while it’s supposed to stop and attack.

For circling, move toward a point offset around the player rather than directly toward the player. Keep that point for a short interval; choosing a new random position every frame will make it jitter. Get the basic chase/attack loop working first, then add circling between attacks.

Here’s a free 2D enemy example showing the chase, wind-up, attack and recovery transitions. It extends EnemyBase in the same project; it isn’t a standalone script or a circling implementation. you can also check out the full guide here

Could you paste your enemy script and say which step fails: finding the player, moving, or attacking?

my problems are mostly in state changes, the enemy won’t switch from wander to chase, and the chase state doesn’t work after adding the state machine and turning the chase into a state, I’m just trying to make guards that only start moving after detecting the player and attack one at a time but all the tutorials I find are for horde shooters or just doesn’t seem to work.

its 3d Ive deleted the enemy stuff to get a fresh start, but I have a copy with a broken enemy code and I do have a post asking to fix it, but I’ve watch through the tutorial twice and nothing’s wrong ether in the script or out.

Could I see the code responsible for this?

What do you mean by “attack one at a time”? Do you want only one guard at a time to move, or am I misunderstanding?

Also, if all you can find are tutorials for horde shooters, you could be thinking about how you can convert the horde shooter AI to what you want. That, or just picking up the basic building blocks from what you’re seeing and using those to make what you want.

I want the enemies to circle the player if one detects them and attack the player at random, I don’t want them all to constantly attack.

I’ve been trying to use the horde tutorials but they’re not that helpful in terms of state changes.

extends Node
class_name  StateMachine

@export var Initialstate: State

var current_state: State = null
var states: Dictionary = {} 


func _ready() -> void:
	for child in get_children():
		if child is State:
			states[child.name.to_lower()] = child
			child.Transitioned.connect(on_child_transitioned)
	
	if Initialstate:
		current_state = Initialstate
		Initialstate.enter()

func _process(delta: float) -> void:
	if current_state:
		current_state.process(delta)

func _physics_process(delta: float) -> void:
	if current_state:
		current_state.process(delta)

func on_child_transitioned(state, new_state_name):
	if state != current_state:
		return
	
	var new_state = states[new_state_name.to_lower()]
	
	if current_state:
		current_state.exit()
	
	new_state.enter()
	current_state = new_state