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()
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.