My navigation agent script doesn't really work, and I don't know what the reason is. Everything I've written so far is right, at least based from the codebases that I've studied. It works by manipulating the transform directly, but I want to do it with the velocity from the kinematic body through moveandslide().
It's even more harder to debug since there's no other resources I can rely to regarding this problem. Note that I've already enabled Avoidance, and this kind of algorithm words in 2D, but somehow not in 3D.
extends KinematicBody
onready var navigationAgent = get_node("NavigationAgent")
onready var navigationServer = get_parent() # Parent must be a navigation node
onready var animationPlayer = get_node("AnimationPlayer")
onready var camera = navigationServer.get_node("Camera")
func _ready():
navigationAgent.set_target_location(global_transform.origin)
func _unhandled_input(event):
if Input.is_action_just_pressed("ui_left_mouse"):
navigationAgent.set_target_location(camera.get_mouse_world_point()) # This bit of code just gets the mouse click position.
func _physics_process(delta):
var nextLocation = navigationAgent.get_next_location()
var direction = global_transform.origin.direction_to(nextLocation)
var velocity = direction * navigationAgent.max_speed
navigationAgent.set_velocity(velocity)
func _on_NavigationAgent_velocity_computed(safe_velocity):
move_and_slide(safe_velocity, Vector3.UP)