I'm making an enemy but he doesn't move

Godot Version

4.4

Question

hello I’m trying to make an enemy i saw a tutorial and made this but the enemy doesn’t move, what I’m making wrong?

``extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

var gravity: float = ProjectSettings.get_setting(“physics/3d/default_gravity”)
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D

var player
var provoked := false
var agrro_range := 12.0

func _ready() → void:
player = get_tree().get_first_node_in_group(“player”)

func _process(delta: float) → void:
if provoked:
navigation_agent_3d.target_position = player.global_position

func _physics_process(delta: float) → void:
var next_position = navigation_agent_3d.get_next_path_position()
# Add the gravity.
if not is_on_floor():
velocity.y-= gravity * delta

var direction = global_position.direction_to(next_position)
var distance = global_position.distance_to(player.global_position)

if distance <= agrro_range:
	provoked=true


if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)
	move_and_slide()

`

either your enemy is not a CharacterBody3D, or you never set a starting path for the enemy.
does the enemy move if you get close?

he should move but he dosent, i did step by step as the tutorial

how do i set a starting path?

I don’t know what tutorial you are using or what your project looks like.
Navigation is not something I ever use, but you most likely need a something for the agent to follow like a path or a navmesh.
navmeshes need to be baked.

I looked at a tutorial real quick:

maybe your agent needs to be children of a navigation region?

what is clear from the code is that the variable provoked must change when the enemy is close to the player, and that sets the target to the player position.
we NEED to know if the enemy moves when the player gets close. that will tell us if this is a problem with the scene setup. because that’s what the code does.

I seem to have completely misunderstood how navigation works in godot and confused it with other engines. I meant that there is no starting patrol for the enemy, but that would require extra code that is not in what you posted.

extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5

var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D

var player
var provoked := false
var agrro_range := 12.0

func _ready() -> void:
	player = get_tree().get_first_node_in_group("player")

func _process(delta: float) -> void:
	if provoked:
		navigation_agent_3d.target_position =  player.global_position

func _physics_process(delta: float) -> void:
	var next_position = navigation_agent_3d.get_next_path_position()
	# Add the gravity.
	if not is_on_floor():
		velocity.y-= gravity * delta


	var direction = global_position.direction_to(next_position)
	var distance = global_position.distance_to(player.global_position)
	
	if distance <= agrro_range:
		provoked=true
	
	
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)
		move_and_slide()

no the enemy stays still even when im standing in front of it

oh this is probably so simple and I didn’t see it the first time xD

your move and slide is inside the else, so it only runs when the enemy is supposed to stop moving.
it should instead look like this:

if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()

just remove one tab by pressing backspace once. move and slider should be outside the if/else.
see if that solves it.

2 Likes

THANK U VERY MUCH IM ETERNALY GRATEFUL :sunglasses: :cowboy_hat_face: