Skip to content

Instantly share code, notes, and snippets.

@bramreth
Created February 17, 2024 11:59
Show Gist options
  • Save bramreth/21a8fcf09a0903a642e751dd9e40080a to your computer and use it in GitHub Desktop.
Save bramreth/21a8fcf09a0903a642e751dd9e40080a to your computer and use it in GitHub Desktop.
Godot 4 Navigation Tutorial Script
extends CharacterBody3D
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
var random_position := Vector3.ZERO
random_position.x = randf_range(-5.0, 5.0)
random_position.z = randf_range(-5.0, 5.0)
navigation_agent_3d.set_target_position(random_position)
func _physics_process(delta: float) -> void:
var destination = navigation_agent_3d.get_next_path_position()
var local_destination = destination - global_position
var direction = local_destination.normalized()
velocity = direction * 5.0
move_and_slide()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment